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 */
017package org.apache.commons.dbcp2.managed;
018
019import java.sql.Connection;
020import java.sql.SQLException;
021import java.util.Collection;
022
023import org.apache.commons.dbcp2.PoolableConnection;
024import org.apache.commons.pool2.ObjectPool;
025
026/**
027 * PoolableConnection that unregisters from TransactionRegistry on Connection real destroy.
028 *
029 * @see PoolableConnection
030 * @since 2.0
031 */
032public class PoolableManagedConnection extends PoolableConnection {
033    private final TransactionRegistry transactionRegistry;
034
035    /**
036     * Create a PoolableManagedConnection.
037     *
038     * @param transactionRegistry
039     *            transaction registry
040     * @param conn
041     *            underlying connection
042     * @param pool
043     *            connection pool
044     */
045    public PoolableManagedConnection(final TransactionRegistry transactionRegistry, final Connection conn,
046            final ObjectPool<PoolableConnection> pool) {
047        this(transactionRegistry, conn, pool, null, true);
048    }
049
050    /**
051     * Create a PoolableManagedConnection.
052     *
053     * @param transactionRegistry
054     *            transaction registry
055     * @param conn
056     *            underlying connection
057     * @param pool
058     *            connection pool
059     * @param disconnectSqlCodes
060     *            SQL_STATE codes considered fatal disconnection errors
061     * @param fastFailValidation
062     *            true means fatal disconnection errors cause subsequent validations to fail immediately (no attempt to
063     *            run query or isValid)
064     */
065    public PoolableManagedConnection(final TransactionRegistry transactionRegistry, final Connection conn,
066            final ObjectPool<PoolableConnection> pool, final Collection<String> disconnectSqlCodes,
067            final boolean fastFailValidation) {
068        super(conn, pool, null, disconnectSqlCodes, fastFailValidation);
069        this.transactionRegistry = transactionRegistry;
070    }
071
072    /**
073     * @return The transaction registry.
074     * @since 2.6.0
075     */
076    public TransactionRegistry getTransactionRegistry() {
077        return transactionRegistry;
078    }
079
080    /**
081     * Actually close the underlying connection.
082     */
083    @Override
084    public void reallyClose() throws SQLException {
085        try {
086            super.reallyClose();
087        } finally {
088            transactionRegistry.unregisterConnection(this);
089        }
090    }
091}