Package org.apache.tomcat.dbcp.dbcp2
Class PoolableConnection
- java.lang.Object
-
- org.apache.tomcat.dbcp.dbcp2.AbandonedTrace
-
- org.apache.tomcat.dbcp.dbcp2.DelegatingConnection<java.sql.Connection>
-
- org.apache.tomcat.dbcp.dbcp2.PoolableConnection
-
- All Implemented Interfaces:
java.lang.AutoCloseable,java.sql.Connection,java.sql.Wrapper,PoolableConnectionMXBean,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 anObjectPoolwhen closed.- Since:
- 2.0
-
-
Constructor Summary
Constructors Constructor Description PoolableConnection(java.sql.Connection conn, ObjectPool<PoolableConnection> pool, javax.management.ObjectName jmxName)Constructs a new instance.PoolableConnection(java.sql.Connection conn, ObjectPool<PoolableConnection> pool, javax.management.ObjectName jmxObjectName, java.util.Collection<java.lang.String> disconnectSqlCodes, boolean fastFailValidation)Constructs a new instance.PoolableConnection(java.sql.Connection conn, ObjectPool<PoolableConnection> pool, javax.management.ObjectName jmxObjectName, java.util.Collection<java.lang.String> disconnectSqlCodes, java.util.Collection<java.lang.String> disconnectionIgnoreSqlCodes, boolean fastFailValidation)Creates a newPoolableConnectioninstance.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidabort(java.util.concurrent.Executor executor)Abort my underlyingConnection.voidclose()Returns this instance to my containing pool.java.util.Collection<java.lang.String>getDisconnectionSqlCodes()Gets the disconnection SQL codes.java.lang.StringgetToString()Gets the value of theDelegatingConnection.toString()method via a bean getter, so it can be read as a property via JMX.protected voidhandleException(java.sql.SQLException e)Handles the given exception by throwing it.booleanisClosed()SeeConnection.isClosed().booleanisFastFailValidation()Tests whether to fail-fast.protected voidpassivate()Clears the list of objects being traced by this object.voidreallyClose()Closes the underlyingConnection.voidsetLastUsed()Sets the time this object was last used to the current time in milliseconds.voidvalidate(java.lang.String sql, int timeoutSeconds)Deprecated.voidvalidate(java.lang.String sql, java.time.Duration timeoutDuration)Validates the connection, using the following algorithm: IffastFailValidation(constructor argument) istrueand this connection has previously thrown a fatal disconnection exception, aSQLExceptionis thrown. Ifsqlis null, the driver's #isValid(timeout)is called.-
Methods inherited from class org.apache.tomcat.dbcp.dbcp2.DelegatingConnection
activate, checkOpen, clearCachedState, clearWarnings, closeInternal, commit, createArrayOf, createBlob, createClob, createNClob, createSQLXML, createStatement, createStatement, createStatement, createStruct, getAutoCommit, getCacheState, getCatalog, getClientInfo, getClientInfo, getDefaultQueryTimeout, getDefaultQueryTimeoutDuration, getDelegate, getDelegateInternal, getHoldability, getInnermostDelegate, getInnermostDelegateInternal, getMetaData, getNetworkTimeout, getSchema, getTransactionIsolation, getTypeMap, getWarnings, handleExceptionNoThrow, innermostDelegateEquals, isClosedInternal, isReadOnly, isValid, isValid, isWrapperFor, nativeSQL, prepareCall, prepareCall, prepareCall, prepareStatement, prepareStatement, prepareStatement, prepareStatement, prepareStatement, prepareStatement, releaseSavepoint, rollback, rollback, setAutoCommit, setCacheState, setCatalog, setClientInfo, setClientInfo, setClosedInternal, setDefaultQueryTimeout, setDefaultQueryTimeout, setDelegate, setHoldability, setNetworkTimeout, setReadOnly, setSavepoint, setSavepoint, setSchema, setTransactionIsolation, setTypeMap, toString, unwrap
-
Methods inherited from class org.apache.tomcat.dbcp.dbcp2.AbandonedTrace
addTrace, clearTrace, close, getLastUsed, getLastUsedInstant, getTrace, removeThisTrace, removeTrace, setLastUsed, setLastUsed
-
Methods inherited from class java.lang.Object
clone, equals, finalize, 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.tomcat.dbcp.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, ObjectPool<PoolableConnection> pool, javax.management.ObjectName jmxName)Constructs a new instance.- Parameters:
conn- my underlying connectionpool- the pool to which I should return when closedjmxName- JMX name
-
PoolableConnection
public PoolableConnection(java.sql.Connection conn, ObjectPool<PoolableConnection> pool, javax.management.ObjectName jmxObjectName, java.util.Collection<java.lang.String> disconnectSqlCodes, boolean fastFailValidation)Constructs a new instance.- Parameters:
conn- my underlying connectionpool- the pool to which I should return when closedjmxObjectName- 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, ObjectPool<PoolableConnection> pool, javax.management.ObjectName jmxObjectName, java.util.Collection<java.lang.String> disconnectSqlCodes, java.util.Collection<java.lang.String> disconnectionIgnoreSqlCodes, boolean fastFailValidation)Creates a newPoolableConnectioninstance.- Parameters:
conn- my underlying connectionpool- the pool to which I should return when closedjmxObjectName- JMX namedisconnectSqlCodes- SQL State codes considered fatal disconnection errorsdisconnectionIgnoreSqlCodes- SQL State codes that should be ignored when determining fatal disconnection errorsfastFailValidation- true means fatal disconnection errors cause subsequent validations to fail immediately (no attempt to run query or isValid)- Since:
- 2.13.0
-
-
Method Detail
-
abort
public void abort(java.util.concurrent.Executor executor) throws java.sql.SQLExceptionAbort my underlyingConnection.- Specified by:
abortin interfacejava.sql.Connection- Overrides:
abortin classDelegatingConnection<java.sql.Connection>- Throws:
java.sql.SQLException- Since:
- 2.9.0
-
close
public void close() throws java.sql.SQLExceptionReturns this instance to my containing pool.- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.sql.Connection- Specified by:
closein interfacePoolableConnectionMXBean- Overrides:
closein classDelegatingConnection<java.sql.Connection>- Throws:
java.sql.SQLException- Ignored here, for subclasses.
-
getDisconnectionSqlCodes
public java.util.Collection<java.lang.String> getDisconnectionSqlCodes()
Gets the disconnection SQL codes.- Returns:
- The disconnection SQL codes.
- Since:
- 2.6.0
-
getToString
public java.lang.String getToString()
Gets the value of theDelegatingConnection.toString()method via a bean getter, so it can be read as a property via JMX.- Specified by:
getToStringin interfacePoolableConnectionMXBean- Returns:
- the value of the
Object.toString().
-
handleException
protected void handleException(java.sql.SQLException e) throws java.sql.SQLExceptionDescription copied from class:DelegatingConnectionHandles the given exception by throwing it.- Overrides:
handleExceptionin classDelegatingConnection<java.sql.Connection>- Parameters:
e- the exception to throw.- Throws:
java.sql.SQLException- the exception to throw.
-
isClosed
public boolean isClosed() throws java.sql.SQLExceptionSeeConnection.isClosed().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:
isClosedin interfacejava.sql.Connection- Specified by:
isClosedin interfacePoolableConnectionMXBean- Overrides:
isClosedin classDelegatingConnection<java.sql.Connection>- Returns:
- See
Connection.isClosed(). - Throws:
java.sql.SQLException- SeeConnection.isClosed().
-
isFastFailValidation
public boolean isFastFailValidation()
Tests whether to fail-fast.- Returns:
- Whether to fail-fast.
- Since:
- 2.6.0
-
passivate
protected void passivate() throws java.sql.SQLExceptionDescription copied from class:DelegatingConnectionClears the list of objects being traced by this object.- Overrides:
passivatein classDelegatingConnection<java.sql.Connection>- Throws:
java.sql.SQLException- Thrown if not all traced objects were closed.
-
reallyClose
public void reallyClose() throws java.sql.SQLExceptionCloses the underlyingConnection.- Specified by:
reallyClosein interfacePoolableConnectionMXBean- Throws:
java.sql.SQLException- Thrown if the connection can be closed.
-
setLastUsed
public void setLastUsed()
Description copied from class:AbandonedTraceSets the time this object was last used to the current time in milliseconds.- Overrides:
setLastUsedin classAbandonedTrace
-
validate
public void validate(java.lang.String sql, java.time.Duration timeoutDuration) throws java.sql.SQLExceptionValidates the connection, using the following algorithm:- If
fastFailValidation(constructor argument) istrueand this connection has previously thrown a fatal disconnection exception, aSQLExceptionis thrown. - If
sqlis null, the driver's #isValid(timeout)is called. If it returnsfalse,SQLExceptionis thrown; otherwise, this method returns successfully. - If
sqlis not null, it is executed as a query and if the resultingResultSetcontains at least one row, this method returns successfully. If not,SQLExceptionis thrown.
- Parameters:
sql- The validation SQL query.timeoutDuration- The validation timeout in seconds.- Throws:
java.sql.SQLException- Thrown when validation fails or an SQLException occurs during validation- Since:
- 2.10.0
- If
-
validate
@Deprecated public void validate(java.lang.String sql, int timeoutSeconds) throws java.sql.SQLExceptionDeprecated.Validates the connection, using the following algorithm:- If
fastFailValidation(constructor argument) istrueand this connection has previously thrown a fatal disconnection exception, aSQLExceptionis thrown. - If
sqlis null, the driver's #isValid(timeout)is called. If it returnsfalse,SQLExceptionis thrown; otherwise, this method returns successfully. - If
sqlis not null, it is executed as a query and if the resultingResultSetcontains at least one row, this method returns successfully. If not,SQLExceptionis thrown.
- Parameters:
sql- The validation SQL query.timeoutSeconds- The validation timeout in seconds.- Throws:
java.sql.SQLException- Thrown when validation fails or an SQLException occurs during validation
- If
-
-