Class PoolableConnection
- All Implemented Interfaces:
AutoCloseable,Connection,Wrapper,PoolableConnectionMXBean,org.apache.commons.pool2.TrackedUse
- Direct Known Subclasses:
PoolableManagedConnection
ObjectPool
when closed.- Since:
- 2.0
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final Collection<String> SQL_STATE codes considered to signal fatal conditions.private final booleanWhether or not to fast fail validation after fatal connection errorsprivate booleanIndicate that unrecoverable SQLException was thrown when using this connection.private final ObjectNameWrapperprivate Stringprivate final Lockprivate static MBeanServerprivate final org.apache.commons.pool2.ObjectPool<PoolableConnection> The pool to which I should return.private PreparedStatementFields inherited from interface java.sql.Connection
TRANSACTION_NONE, TRANSACTION_READ_COMMITTED, TRANSACTION_READ_UNCOMMITTED, TRANSACTION_REPEATABLE_READ, TRANSACTION_SERIALIZABLE -
Constructor Summary
ConstructorsConstructorDescriptionPoolableConnection(Connection conn, org.apache.commons.pool2.ObjectPool<PoolableConnection> pool, ObjectName jmxName) PoolableConnection(Connection conn, org.apache.commons.pool2.ObjectPool<PoolableConnection> pool, ObjectName jmxObjectName, Collection<String> disconnectSqlCodes, boolean fastFailValidation) -
Method Summary
Modifier and TypeMethodDescriptionvoidAbort my underlyingConnection.voidclose()Returns me to my pool.Expose theDelegatingConnection.toString()method via a bean getter, so it can be read as a property via JMX.protected voidHandles the given exception by throwing it.booleanisClosed()(package private) booleanChecks the SQLState of the input exception.boolean(package private) booleanChecks the SQLState of the input exception and any nested SQLExceptions it wraps.protected voidvoidActually close my underlyingConnection.voidSets the time this object was last used to the current time in milliseconds.voidDeprecated.voidValidates 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.commons.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, unwrapMethods inherited from class org.apache.commons.dbcp2.AbandonedTrace
add, addTrace, clearTrace, close, getLastUsed, getLastUsedInstant, getTrace, removeThisTrace, removeTrace, setLastUsed, setLastUsedMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface org.apache.commons.dbcp2.PoolableConnectionMXBean
clearCachedState, clearWarnings, getAutoCommit, getCacheState, getCatalog, getHoldability, getSchema, getTransactionIsolation, isReadOnly, setAutoCommit, setCacheState, setCatalog, setHoldability, setReadOnly, setSchema, setTransactionIsolation
-
Field Details
-
MBEAN_SERVER
-
pool
The pool to which I should return. -
jmxObjectName
-
validationPreparedStatement
-
lastValidationSql
-
fatalSqlExceptionThrown
private boolean fatalSqlExceptionThrownIndicate that unrecoverable SQLException was thrown when using this connection. Such a connection should be considered broken and not pass validation in the future. -
disconnectionSqlCodes
SQL_STATE codes considered to signal fatal conditions. Overrides the defaults inUtils.getDisconnectionSqlCodes()(plus anything starting withUtils.DISCONNECTION_SQL_CODE_PREFIX). -
fastFailValidation
private final boolean fastFailValidationWhether or not to fast fail validation after fatal connection errors -
lock
-
-
Constructor Details
-
PoolableConnection
public PoolableConnection(Connection conn, org.apache.commons.pool2.ObjectPool<PoolableConnection> pool, ObjectName jmxName) - Parameters:
conn- my underlying connectionpool- the pool to which I should return when closedjmxName- JMX name
-
PoolableConnection
public PoolableConnection(Connection conn, org.apache.commons.pool2.ObjectPool<PoolableConnection> pool, ObjectName jmxObjectName, Collection<String> disconnectSqlCodes, boolean fastFailValidation) - 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)
-
-
Method Details
-
abort
Abort my underlyingConnection.- Specified by:
abortin interfaceConnection- Overrides:
abortin classDelegatingConnection<Connection>- Throws:
SQLException- Since:
- 2.9.0
-
close
Returns me to my pool.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceConnection- Specified by:
closein interfacePoolableConnectionMXBean- Overrides:
closein classDelegatingConnection<Connection>- Throws:
SQLException- Ignored here, for subclasses.
-
getDisconnectionSqlCodes
- Returns:
- The disconnection SQL codes.
- Since:
- 2.6.0
-
getToString
Expose theDelegatingConnection.toString()method via a bean getter, so it can be read as a property via JMX.- Specified by:
getToStringin interfacePoolableConnectionMXBean
-
handleException
Description copied from class:DelegatingConnectionHandles the given exception by throwing it.- Overrides:
handleExceptionin classDelegatingConnection<Connection>- Parameters:
e- the exception to throw.- Throws:
SQLException- the exception to throw.
-
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 interfaceConnection- Specified by:
isClosedin interfacePoolableConnectionMXBean- Overrides:
isClosedin classDelegatingConnection<Connection>- Throws:
SQLException
-
isDisconnectionSqlException
Checks the SQLState of the input exception.If
disconnectionSqlCodeshas been set, sql states are compared to those in the configured list of fatal exception codes. If this property is not set, codes are compared against the default codes inUtils.getDisconnectionSqlCodes()and in this case anything starting with #{link Utils.DISCONNECTION_SQL_CODE_PREFIX} is considered a disconnection.- Parameters:
e- SQLException to be examined- Returns:
- true if the exception signals a disconnection
-
isFastFailValidation
public boolean isFastFailValidation()- Returns:
- Whether to fail-fast.
- Since:
- 2.6.0
-
isFatalException
Checks the SQLState of the input exception and any nested SQLExceptions it wraps.If
disconnectionSqlCodeshas been set, sql states are compared to those in the configured list of fatal exception codes. If this property is not set, codes are compared against the default codes inUtils.getDisconnectionSqlCodes()and in this case anything starting with #{link Utils.DISCONNECTION_SQL_CODE_PREFIX} is considered a disconnection.- Parameters:
e- SQLException to be examined- Returns:
- true if the exception signals a disconnection
-
passivate
- Overrides:
passivatein classDelegatingConnection<Connection>- Throws:
SQLException
-
reallyClose
Actually close my underlyingConnection.- Specified by:
reallyClosein interfacePoolableConnectionMXBean- Throws:
SQLException
-
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
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.timeoutDuration- The validation timeout in seconds.- Throws:
SQLException- Thrown when validation fails or an SQLException occurs during validation- Since:
- 2.10.0
- If
-
validate
Deprecated.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:
SQLException- Thrown when validation fails or an SQLException occurs during validation
- If
-
validate(String, Duration).