PoolableManagedConnection.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.dbcp2.managed;

  18. import java.sql.Connection;
  19. import java.sql.SQLException;
  20. import java.util.Collection;

  21. import org.apache.commons.dbcp2.PoolableConnection;
  22. import org.apache.commons.pool2.ObjectPool;

  23. /**
  24.  * PoolableConnection that unregisters from TransactionRegistry on Connection real destroy.
  25.  *
  26.  * @see PoolableConnection
  27.  * @since 2.0
  28.  */
  29. public class PoolableManagedConnection extends PoolableConnection {
  30.     private final TransactionRegistry transactionRegistry;

  31.     /**
  32.      * Creates a PoolableManagedConnection.
  33.      *
  34.      * @param transactionRegistry
  35.      *            transaction registry
  36.      * @param conn
  37.      *            underlying connection
  38.      * @param pool
  39.      *            connection pool
  40.      */
  41.     public PoolableManagedConnection(final TransactionRegistry transactionRegistry, final Connection conn, final ObjectPool<PoolableConnection> pool) {
  42.         this(transactionRegistry, conn, pool, null, true);
  43.     }

  44.     /**
  45.      * Creates a PoolableManagedConnection.
  46.      *
  47.      * @param transactionRegistry
  48.      *            transaction registry
  49.      * @param conn
  50.      *            underlying connection
  51.      * @param pool
  52.      *            connection pool
  53.      * @param disconnectSqlCodes
  54.      *            SQL State codes considered fatal disconnection errors
  55.      * @param fastFailValidation
  56.      *            true means fatal disconnection errors cause subsequent validations to fail immediately (no attempt to
  57.      *            run query or isValid)
  58.      */
  59.     public PoolableManagedConnection(final TransactionRegistry transactionRegistry, final Connection conn, final ObjectPool<PoolableConnection> pool,
  60.             final Collection<String> disconnectSqlCodes, final boolean fastFailValidation) {
  61.         this(transactionRegistry, conn, pool, disconnectSqlCodes, null, fastFailValidation);
  62.     }

  63.     /**
  64.      * Creates a PoolableManagedConnection.
  65.      *
  66.      * @param transactionRegistry
  67.      *            transaction registry
  68.      * @param conn
  69.      *            underlying connection
  70.      * @param pool
  71.      *            connection pool
  72.      * @param disconnectSqlCodes
  73.      *            SQL State codes considered fatal disconnection errors
  74.      * @param disconnectionIgnoreSqlCodes
  75.      *            SQL State codes considered fatal disconnection errors
  76.      * @param fastFailValidation
  77.      *            true means fatal disconnection errors cause subsequent validations to fail immediately (no attempt to
  78.      *            run query or isValid)
  79.      * @since 2.13.0
  80.      */
  81.     public PoolableManagedConnection(final TransactionRegistry transactionRegistry, final Connection conn, final ObjectPool<PoolableConnection> pool,
  82.             final Collection<String> disconnectSqlCodes, final Collection<String> disconnectionIgnoreSqlCodes, final boolean fastFailValidation) {
  83.         super(conn, pool, null, disconnectSqlCodes, disconnectionIgnoreSqlCodes, fastFailValidation);
  84.         this.transactionRegistry = transactionRegistry;
  85.     }

  86.     /**
  87.      * Gets the transaction registry.
  88.      *
  89.      * @return The transaction registry.
  90.      * @since 2.6.0
  91.      */
  92.     public TransactionRegistry getTransactionRegistry() {
  93.         return transactionRegistry;
  94.     }

  95.     /**
  96.      * Actually close the underlying connection.
  97.      */
  98.     @Override
  99.     public void reallyClose() throws SQLException {
  100.         try {
  101.             super.reallyClose();
  102.         } finally {
  103.             transactionRegistry.unregisterConnection(this);
  104.         }
  105.     }
  106. }