    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 *      https://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     * Creates 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, final ObjectPool<PoolableConnection> pool) {
    046        this(transactionRegistry, conn, pool, null, true);
    047    }
    048
    049    /**
    050     * Creates a PoolableManagedConnection.
    051     *
    052     * @param transactionRegistry
    053     *            transaction registry
    054     * @param conn
    055     *            underlying connection
    056     * @param pool
    057     *            connection pool
    058     * @param disconnectSqlCodes
    059     *            SQL State codes considered fatal disconnection errors
    060     * @param fastFailValidation
    061     *            true means fatal disconnection errors cause subsequent validations to fail immediately (no attempt to
    062     *            run query or isValid)
    063     */
    064    public PoolableManagedConnection(final TransactionRegistry transactionRegistry, final Connection conn, final ObjectPool<PoolableConnection> pool,
    065            final Collection<String> disconnectSqlCodes, final boolean fastFailValidation) {
    066        this(transactionRegistry, conn, pool, disconnectSqlCodes, null, fastFailValidation);
    067    }
    068
    069    /**
    070     * Creates a PoolableManagedConnection.
    071     *
    072     * @param transactionRegistry
    073     *            transaction registry
    074     * @param conn
    075     *            underlying connection
    076     * @param pool
    077     *            connection pool
    078     * @param disconnectSqlCodes
    079     *            SQL State codes considered fatal disconnection errors
    080     * @param disconnectionIgnoreSqlCodes
    081     *            SQL State codes considered fatal disconnection errors
    082     * @param fastFailValidation
    083     *            true means fatal disconnection errors cause subsequent validations to fail immediately (no attempt to
    084     *            run query or isValid)
    085     * @since 2.13.0
    086     */
    087    public PoolableManagedConnection(final TransactionRegistry transactionRegistry, final Connection conn, final ObjectPool<PoolableConnection> pool,
    088            final Collection<String> disconnectSqlCodes, final Collection<String> disconnectionIgnoreSqlCodes, final boolean fastFailValidation) {
    089        super(conn, pool, null, disconnectSqlCodes, disconnectionIgnoreSqlCodes, fastFailValidation);
    090        this.transactionRegistry = transactionRegistry;
    091    }
    092
    093    /**
    094     * Gets the transaction registry.
    095     *
    096     * @return The transaction registry.
    097     * @since 2.6.0
    098     */
    099    public TransactionRegistry getTransactionRegistry() {
    100        return transactionRegistry;
    101    }
    102
    103    /**
    104     * Actually close the underlying connection.
    105     */
    106    @Override
    107    public void reallyClose() throws SQLException {
    108        try {
    109            super.reallyClose();
    110        } finally {
    111            transactionRegistry.unregisterConnection(this);
    112        }
    113    }
    114}