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 19 import java.sql.Connection; 20 import java.sql.SQLException; 21 import java.util.Collection; 22 23 import org.apache.commons.dbcp2.PoolableConnection; 24 import org.apache.commons.pool2.ObjectPool; 25 26 /** 27 * PoolableConnection that unregisters from TransactionRegistry on Connection real destroy. 28 * 29 * @see PoolableConnection 30 * @since 2.0 31 */ 32 public class PoolableManagedConnection extends PoolableConnection { 33 private final TransactionRegistry transactionRegistry; 34 35 /** 36 * Creates a PoolableManagedConnection. 37 * 38 * @param transactionRegistry 39 * transaction registry 40 * @param conn 41 * underlying connection 42 * @param pool 43 * connection pool 44 */ 45 public PoolableManagedConnection(final TransactionRegistry transactionRegistry, final Connection conn, final ObjectPool<PoolableConnection> pool) { 46 this(transactionRegistry, conn, pool, null, true); 47 } 48 49 /** 50 * Creates a PoolableManagedConnection. 51 * 52 * @param transactionRegistry 53 * transaction registry 54 * @param conn 55 * underlying connection 56 * @param pool 57 * connection pool 58 * @param disconnectSqlCodes 59 * SQL State codes considered fatal disconnection errors 60 * @param fastFailValidation 61 * true means fatal disconnection errors cause subsequent validations to fail immediately (no attempt to 62 * run query or isValid) 63 */ 64 public PoolableManagedConnection(final TransactionRegistry transactionRegistry, final Connection conn, final ObjectPool<PoolableConnection> pool, 65 final Collection<String> disconnectSqlCodes, final boolean fastFailValidation) { 66 this(transactionRegistry, conn, pool, disconnectSqlCodes, null, fastFailValidation); 67 } 68 69 /** 70 * Creates a PoolableManagedConnection. 71 * 72 * @param transactionRegistry 73 * transaction registry 74 * @param conn 75 * underlying connection 76 * @param pool 77 * connection pool 78 * @param disconnectSqlCodes 79 * SQL State codes considered fatal disconnection errors 80 * @param disconnectionIgnoreSqlCodes 81 * SQL State codes considered fatal disconnection errors 82 * @param fastFailValidation 83 * true means fatal disconnection errors cause subsequent validations to fail immediately (no attempt to 84 * run query or isValid) 85 * @since 2.13.0 86 */ 87 public PoolableManagedConnection(final TransactionRegistry transactionRegistry, final Connection conn, final ObjectPool<PoolableConnection> pool, 88 final Collection<String> disconnectSqlCodes, final Collection<String> disconnectionIgnoreSqlCodes, final boolean fastFailValidation) { 89 super(conn, pool, null, disconnectSqlCodes, disconnectionIgnoreSqlCodes, fastFailValidation); 90 this.transactionRegistry = transactionRegistry; 91 } 92 93 /** 94 * Gets the transaction registry. 95 * 96 * @return The transaction registry. 97 * @since 2.6.0 98 */ 99 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 }