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 * Create 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, 46 final ObjectPool<PoolableConnection> pool) { 47 this(transactionRegistry, conn, pool, null, true); 48 } 49 50 /** 51 * Create a PoolableManagedConnection. 52 * 53 * @param transactionRegistry 54 * transaction registry 55 * @param conn 56 * underlying connection 57 * @param pool 58 * connection pool 59 * @param disconnectSqlCodes 60 * SQL_STATE codes considered fatal disconnection errors 61 * @param fastFailValidation 62 * true means fatal disconnection errors cause subsequent validations to fail immediately (no attempt to 63 * run query or isValid) 64 */ 65 public PoolableManagedConnection(final TransactionRegistry transactionRegistry, final Connection conn, 66 final ObjectPool<PoolableConnection> pool, final Collection<String> disconnectSqlCodes, 67 final boolean fastFailValidation) { 68 super(conn, pool, null, disconnectSqlCodes, fastFailValidation); 69 this.transactionRegistry = transactionRegistry; 70 } 71 72 /** 73 * @return The transaction registry. 74 * @since 2.6.0 75 */ 76 public TransactionRegistry getTransactionRegistry() { 77 return transactionRegistry; 78 } 79 80 /** 81 * Actually close the underlying connection. 82 */ 83 @Override 84 public void reallyClose() throws SQLException { 85 try { 86 super.reallyClose(); 87 } finally { 88 transactionRegistry.unregisterConnection(this); 89 } 90 } 91 }