Class PoolableConnection
- java.lang.Object
-
- org.datanucleus.store.rdbms.datasource.dbcp2.AbandonedTrace
-
- org.datanucleus.store.rdbms.datasource.dbcp2.DelegatingConnection<java.sql.Connection>
-
- org.datanucleus.store.rdbms.datasource.dbcp2.PoolableConnection
-
- All Implemented Interfaces:
java.lang.AutoCloseable,java.sql.Connection,java.sql.Wrapper,TrackedUse,PoolableConnectionMXBean
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
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.Collection<java.lang.String>disconnectionSqlCodesSQL_STATE codes considered to signal fatal conditions.private booleanfastFailValidationWhether or not to fast fail validation after fatal connection errorsprivate booleanfatalSqlExceptionThrownIndicate that unrecoverable SQLException was thrown when using this connection.private ObjectNameWrapperjmxObjectNameprivate java.lang.StringlastValidationSqlprivate static javax.management.MBeanServerMBEAN_SERVERprivate ObjectPool<PoolableConnection>poolThe pool to which I should return.private java.sql.PreparedStatementvalidationPreparedStatement
-
Constructor Summary
Constructors Constructor Description PoolableConnection(java.sql.Connection conn, ObjectPool<PoolableConnection> pool, javax.management.ObjectName jmxName)PoolableConnection(java.sql.Connection conn, ObjectPool<PoolableConnection> pool, javax.management.ObjectName jmxObjectName, java.util.Collection<java.lang.String> disconnectSqlCodes, boolean fastFailValidation)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Returns me to my pool.java.util.Collection<java.lang.String>getDisconnectionSqlCodes()java.lang.StringgetToString()Expose theDelegatingConnection.toString()method via a bean getter so it can be read as a property via JMX.protected voidhandleException(java.sql.SQLException e)booleanisClosed()private booleanisDisconnectionSqlException(java.sql.SQLException e)Checks the SQLState of the input exception and any nested SQLExceptions it wraps.booleanisFastFailValidation()protected voidpassivate()voidreallyClose()Actually close my underlyingConnection.voidvalidate(java.lang.String sql, int timeoutSeconds)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.datanucleus.store.rdbms.datasource.dbcp2.DelegatingConnection
abort, activate, checkOpen, clearCachedState, clearWarnings, closeInternal, commit, createArrayOf, createBlob, createClob, createNClob, createSQLXML, createStatement, createStatement, createStatement, createStruct, getAutoCommit, getCacheState, getCatalog, getClientInfo, getClientInfo, getDefaultQueryTimeout, getDelegate, getDelegateInternal, getHoldability, getInnermostDelegate, getInnermostDelegateInternal, getMetaData, getNetworkTimeout, getSchema, getTransactionIsolation, getTypeMap, getWarnings, handleExceptionNoThrow, innermostDelegateEquals, isClosedInternal, isReadOnly, isValid, isWrapperFor, nativeSQL, prepareCall, prepareCall, prepareCall, prepareStatement, prepareStatement, prepareStatement, prepareStatement, prepareStatement, prepareStatement, releaseSavepoint, rollback, rollback, setAutoCommit, setCacheState, setCatalog, setClientInfo, setClientInfo, setClosedInternal, setDefaultQueryTimeout, setDelegate, setHoldability, setNetworkTimeout, setReadOnly, setSavepoint, setSavepoint, setSchema, setTransactionIsolation, setTypeMap, toString, unwrap
-
Methods inherited from class org.datanucleus.store.rdbms.datasource.dbcp2.AbandonedTrace
addTrace, clearTrace, getLastUsed, 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.datanucleus.store.rdbms.datasource.dbcp2.PoolableConnectionMXBean
clearCachedState, clearWarnings, getAutoCommit, getCacheState, getCatalog, getHoldability, getSchema, getTransactionIsolation, isReadOnly, setAutoCommit, setCacheState, setCatalog, setHoldability, setReadOnly, setSchema, setTransactionIsolation
-
-
-
-
Field Detail
-
MBEAN_SERVER
private static javax.management.MBeanServer MBEAN_SERVER
-
pool
private final ObjectPool<PoolableConnection> pool
The pool to which I should return.
-
jmxObjectName
private final ObjectNameWrapper jmxObjectName
-
validationPreparedStatement
private java.sql.PreparedStatement validationPreparedStatement
-
lastValidationSql
private java.lang.String lastValidationSql
-
fatalSqlExceptionThrown
private boolean fatalSqlExceptionThrown
Indicate that unrecoverable SQLException was thrown when using this connection. Such a connection should be considered broken and not pass validation in the future.
-
disconnectionSqlCodes
private final java.util.Collection<java.lang.String> disconnectionSqlCodes
SQL_STATE codes considered to signal fatal conditions. Overrides the defaults inUtils.DISCONNECTION_SQL_CODES(plus anything starting withUtils.DISCONNECTION_SQL_CODE_PREFIX).
-
fastFailValidation
private final boolean fastFailValidation
Whether or not to fast fail validation after fatal connection errors
-
-
Constructor Detail
-
PoolableConnection
public PoolableConnection(java.sql.Connection conn, ObjectPool<PoolableConnection> pool, javax.management.ObjectName jmxObjectName, java.util.Collection<java.lang.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)
-
PoolableConnection
public PoolableConnection(java.sql.Connection conn, 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
-
passivate
protected void passivate() throws java.sql.SQLException- Overrides:
passivatein classDelegatingConnection<java.sql.Connection>- Throws:
java.sql.SQLException
-
isClosed
public boolean isClosed() throws java.sql.SQLExceptionThis 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>- Throws:
java.sql.SQLException
-
close
public void close() throws java.sql.SQLExceptionReturns me to my 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
-
reallyClose
public void reallyClose() throws java.sql.SQLExceptionActually close my underlyingConnection.- Specified by:
reallyClosein 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:
getToStringin interfacePoolableConnectionMXBean
-
validate
public void validate(java.lang.String sql, int timeoutSeconds) 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.timeoutSeconds- The validation timeout in seconds.- Throws:
java.sql.SQLException- Thrown when validation fails or an SQLException occurs during validation
- If
-
isDisconnectionSqlException
private boolean isDisconnectionSqlException(java.sql.SQLException e)
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.DISCONNECTION_SQL_CODESand 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
-
handleException
protected void handleException(java.sql.SQLException e) throws java.sql.SQLException- Overrides:
handleExceptionin classDelegatingConnection<java.sql.Connection>- Throws:
java.sql.SQLException
-
getDisconnectionSqlCodes
public java.util.Collection<java.lang.String> getDisconnectionSqlCodes()
- Returns:
- The disconnection SQL codes.
- Since:
- 2.6.0
-
isFastFailValidation
public boolean isFastFailValidation()
- Returns:
- Whether to fail-fast.
- Since:
- 2.6.0
-
-