001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    package org.apache.commons.dbcp.managed;
019    
020    import java.sql.Connection;
021    import java.sql.SQLException;
022    
023    import org.apache.commons.dbcp.AbandonedConfig;
024    import org.apache.commons.dbcp.PoolableConnection;
025    import org.apache.commons.pool.ObjectPool;
026    
027    /**
028     * PoolableConnection that unregisters from TransactionRegistry on Connection real destroy.
029     * 
030     * @see PoolableConnection
031     * @version $Revision: 892307 $ $Date: 2013-12-31 23:27:28 +0000 (Tue, 31 Dec 2013) $
032     */
033    public class PoolableManagedConnection extends PoolableConnection {
034        private final TransactionRegistry transactionRegistry;
035    
036        /**
037         * Create a PoolableManagedConnection.
038         * 
039         * @param transactionRegistry transaction registry 
040         * @param conn underlying connection
041         * @param pool connection pool
042         * @param config abandoned configuration settings
043         */
044        public PoolableManagedConnection(TransactionRegistry transactionRegistry, Connection conn, ObjectPool pool, AbandonedConfig config) {
045            super(conn, pool, config);
046            this.transactionRegistry = transactionRegistry;
047        }
048        
049        /**
050         * Create a PoolableManagedConnection.
051         * 
052         * @param transactionRegistry transaction registry 
053         * @param conn underlying connection
054         * @param pool connection pool
055         */
056        public PoolableManagedConnection(TransactionRegistry transactionRegistry, Connection conn, ObjectPool pool) {
057            super(conn, pool);
058            this.transactionRegistry = transactionRegistry;
059        }
060    
061    
062        /**
063         * Actually close the underlying connection.
064         */
065        public void reallyClose() throws SQLException {
066            try {
067                super.reallyClose();
068            } finally {
069                transactionRegistry.unregisterConnection(this);
070            }
071        }
072    }