Package org.apache.commons.dbcp2
Class PoolableConnection
- java.lang.Object
-
- org.apache.commons.dbcp2.AbandonedTrace
-
- org.apache.commons.dbcp2.DelegatingConnection<java.sql.Connection>
-
- org.apache.commons.dbcp2.PoolableConnection
-
- All Implemented Interfaces:
java.lang.AutoCloseable
,java.sql.Connection
,java.sql.Wrapper
,PoolableConnectionMXBean
,org.apache.commons.pool2.TrackedUse
- Direct Known Subclasses:
PoolableManagedConnection
public class PoolableConnection extends DelegatingConnection<java.sql.Connection> implements PoolableConnectionMXBean
A delegating connection that, rather than closing the underlying connection, returns itself to anObjectPool
when closed.- Since:
- 2.0
- Version:
- $Id: PoolableConnection.java 1658644 2015-02-10 08:59:07Z tn $
- Author:
- Rodney Waldhoff, Glenn L. Nielsen, James House
-
-
Constructor Summary
Constructors Constructor Description PoolableConnection(java.sql.Connection conn, org.apache.commons.pool2.ObjectPool<PoolableConnection> pool, javax.management.ObjectName jmxName)
PoolableConnection(java.sql.Connection conn, org.apache.commons.pool2.ObjectPool<PoolableConnection> pool, javax.management.ObjectName jmxName, java.util.Collection<java.lang.String> disconnectSqlCodes, boolean fastFailValidation)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
Returns me to my pool.java.lang.String
getToString()
Expose theDelegatingConnection.toString()
method via a bean getter so it can be read as a property via JMX.boolean
isClosed()
void
reallyClose()
Actually close my underlyingConnection
.void
validate(java.lang.String sql, int timeout)
Validates the connection, using the following algorithm: IffastFailValidation
(constructor argument) istrue
and this connection has previously thrown a fatal disconnection exception, aSQLException
is thrown.-
Methods inherited from class org.apache.commons.dbcp2.DelegatingConnection
abort, clearCachedState, clearWarnings, commit, createArrayOf, createBlob, createClob, createNClob, createSQLXML, createStatement, createStatement, createStatement, createStruct, getAutoCommit, getCacheState, getCatalog, getClientInfo, getClientInfo, getDefaultQueryTimeout, getDelegate, getHoldability, getInnermostDelegate, getInnermostDelegateInternal, getMetaData, getNetworkTimeout, getSchema, getTransactionIsolation, getTypeMap, getWarnings, innermostDelegateEquals, isReadOnly, isValid, isWrapperFor, nativeSQL, prepareCall, prepareCall, prepareCall, prepareStatement, prepareStatement, prepareStatement, prepareStatement, prepareStatement, prepareStatement, releaseSavepoint, rollback, rollback, setAutoCommit, setCacheState, setCatalog, setClientInfo, setClientInfo, setDefaultQueryTimeout, setDelegate, setHoldability, setNetworkTimeout, setReadOnly, setSavepoint, setSavepoint, setSchema, setTransactionIsolation, setTypeMap, toString, unwrap
-
Methods inherited from class org.apache.commons.dbcp2.AbandonedTrace
getLastUsed
-
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.sql.Connection
beginRequest, endRequest, setShardingKey, setShardingKey, setShardingKeyIfValid, setShardingKeyIfValid
-
Methods inherited from interface org.apache.commons.dbcp2.PoolableConnectionMXBean
clearCachedState, clearWarnings, getAutoCommit, getCacheState, getCatalog, getHoldability, getSchema, getTransactionIsolation, isReadOnly, setAutoCommit, setCacheState, setCatalog, setHoldability, setReadOnly, setSchema, setTransactionIsolation
-
-
-
-
Constructor Detail
-
PoolableConnection
public PoolableConnection(java.sql.Connection conn, org.apache.commons.pool2.ObjectPool<PoolableConnection> pool, javax.management.ObjectName jmxName, java.util.Collection<java.lang.String> disconnectSqlCodes, boolean fastFailValidation)
- Parameters:
conn
- my underlying connectionpool
- the pool to which I should return when closedjmxName
- JMX namedisconnectSqlCodes
- SQL_STATE codes considered fatal disconnection errorsfastFailValidation
- true means fatal disconnection errors cause subsequent validations to fail immediately (no attempt to run query or isValid)
-
PoolableConnection
public PoolableConnection(java.sql.Connection conn, org.apache.commons.pool2.ObjectPool<PoolableConnection> pool, javax.management.ObjectName jmxName)
- Parameters:
conn
- my underlying connectionpool
- the pool to which I should return when closedjmxName
- JMX name
-
-
Method Detail
-
isClosed
public boolean isClosed() throws java.sql.SQLException
This method should not be used by a client to determine whether or not a connection should be return to the connection pool (by calling
close()
). Clients should always attempt to return a connection to the pool once it is no longer required.- Specified by:
isClosed
in interfacejava.sql.Connection
- Specified by:
isClosed
in interfacePoolableConnectionMXBean
- Overrides:
isClosed
in classDelegatingConnection<java.sql.Connection>
- Throws:
java.sql.SQLException
-
close
public void close() throws java.sql.SQLException
Returns me to my pool.- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.sql.Connection
- Specified by:
close
in interfacePoolableConnectionMXBean
- Overrides:
close
in classDelegatingConnection<java.sql.Connection>
- Throws:
java.sql.SQLException
-
reallyClose
public void reallyClose() throws java.sql.SQLException
Actually close my underlyingConnection
.- Specified by:
reallyClose
in interfacePoolableConnectionMXBean
- Throws:
java.sql.SQLException
-
getToString
public java.lang.String getToString()
Expose theDelegatingConnection.toString()
method via a bean getter so it can be read as a property via JMX.- Specified by:
getToString
in interfacePoolableConnectionMXBean
-
validate
public void validate(java.lang.String sql, int timeout) throws java.sql.SQLException
Validates the connection, using the following algorithm:- If
fastFailValidation
(constructor argument) istrue
and this connection has previously thrown a fatal disconnection exception, aSQLException
is thrown. - If
sql
is null, the driver's #isValid(timeout)
is called. If it returnsfalse
,SQLException
is thrown; otherwise, this method returns successfully. - If
sql
is not null, it is executed as a query and if the resultingResultSet
contains at least one row, this method returns successfully. If not,SQLException
is thrown.
- Parameters:
sql
- validation querytimeout
- validation timeout- Throws:
java.sql.SQLException
- if validation fails or an SQLException occurs during validation
- If
-
-