Class ConnectionState
- All Implemented Interfaces:
InvocationHandler
ConnectionPool is optimized to do as little work as possible. The pool itself
doesn't remember settings like Connection.setAutoCommit(boolean),
Connection.setReadOnly(boolean), Connection.setCatalog(String) or
Connection.setTransactionIsolation(int). It relies on the application to remember how and when these
settings have been applied. In the cases where the application code doesn't know or want to keep track of the state,
this interceptor helps cache the state, and it also avoids roundtrips to the database asking for it.
Caching is only half the role. Once this interceptor is configured, the pool hands it the enforcement of the four
configured defaults - defaultAutoCommit, defaultReadOnly, defaultTransactionIsolation and
defaultCatalog: PooledConnection.connect() no longer applies them at
connection creation, and reset(ConnectionPool, PooledConnection) applies them to every borrow and re-applies
whatever a previous borrower changed, on the terms that method states.
A subclass that overrides reset(ConnectionPool, PooledConnection) must call the superclass, or apply those
defaults itself. The pool recognises this class by type and, having found it, leaves the defaults to the interceptor
alone; an override that skips the superclass therefore silently drops both the connect-time application and the
borrow-time restore, warning no one.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected BooleanCached auto-commit state of the connection.protected StringCached catalog (database) name of the connection.protected BooleanCached read-only state of the connection.protected final String[]Method names for reading connection state.protected IntegerCached transaction isolation level of the connection.protected final String[]Method names for writing connection state, indexed asreadState.Fields inherited from class JdbcInterceptor
ABORT_VAL, CLOSE_VAL, EQUALS_VAL, GETCONNECTION_VAL, HASHCODE_VAL, ISCLOSED_VAL, ISVALID_VAL, ISWRAPPERFOR_VAL, properties, TOSTRING_VAL, UNWRAP_VAL -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoiddisconnected(ConnectionPool parent, PooledConnection con, boolean finalizing) Called whenConnection.close()is called on the underlying connection.Gets invoked each time an operation onConnectionis invoked.voidreset(ConnectionPool parent, PooledConnection con) Restores the connection state the pool was configured to enforce.Methods inherited from class JdbcInterceptor
compare, compare, getNext, getProperties, isUseEquals, poolClosed, poolStarted, setNext, setProperties, setUseEquals, unregisterMBean
-
Field Details
-
readState
Method names for reading connection state. The index into this array and intowriteStateis the identity of a tracked state across the class: 0 is the auto-commit state, 1 the transaction isolation level, 2 the read only state and 3 the catalog. The entry at an index ofwriteStateis the setter whose value the cache field of the same index holds, andinvoke(Object, Method, Object[])matches both arrays by index against the cache fields. Reordering one array without the other would map a setter's value into the wrong field; adding a tracked state means all three - both arrays, the cache field, and the matching ininvoke(Object, Method, Object[])- at once. -
writeState
-
autoCommit
Cached auto-commit state of the connection. -
transactionIsolation
Cached transaction isolation level of the connection. -
readOnly
Cached read-only state of the connection. -
catalog
Cached catalog (database) name of the connection.
-
-
Constructor Details
-
ConnectionState
public ConnectionState()Creates a new ConnectionState interceptor instance.
-
-
Method Details
-
reset
Restores the connection state the pool was configured to enforce. The pool invokes this on every borrow, before the connection is handed to its next borrower.When any restore is pending, a transaction the previous borrower left open is terminated first: isolation and read only are transaction scoped on many drivers, an open transaction would absorb the restores, and restoring auto-commit would silently commit the previous borrower's work. Failing to terminate that transaction, and a confirmed refusal to restore the read-only, auto-commit or catalog state, fail the borrow and retire the session - a connection known to sit in a state other than the configured one must not reach a borrower. A refusal the driver blames on the session's transaction state is confirmed first, against a session freed of it (
confirmAgainstFreedSession(PooledConnection, SQLException, ConnectionState.RestoreAttempt)).A transaction isolation level the driver refuses on a freed session is the one confirmed failure that does not retire the connection: the level is recorded (
refusedIsolationLevel) and stops counting as a pending restore, so later borrows neither retry it nor pay a transaction termination on its behalf. When nothing needs restoring, nothing is terminated - a pool that uses this interceptor only to cache the state pays nothing.- Specified by:
resetin classJdbcInterceptor- Parameters:
parent- the pool the connection belongs to, ornullwhen the interceptor chain is being torn downcon- the pooled connection being set up for its next borrower
-
disconnected
Description copied from class:JdbcInterceptorCalled whenConnection.close()is called on the underlying connection. This is to notify the interceptors, that the physical connection has been released. Implementation of this method should be thought through with care, as no actions should trigger an exception. An interceptor that nevertheless throws is isolated: the exception is logged and ignored, the remaining interceptors in the chain are still notified, and the physical connection is closed as it would have been. As forJdbcInterceptor.reset(ConnectionPool, PooledConnection), a hook must not rewire the chain from this callback: chain topology is owned by the pool.- Overrides:
disconnectedin classJdbcInterceptor- Parameters:
parent- - the connection pool that this connection belongs tocon- - the pooled connection that holds this connectionfinalizing- - if this connection is finalizing. True means that the pooled connection will not reconnect the underlying connection
-
invoke
Description copied from class:JdbcInterceptorGets invoked each time an operation onConnectionis invoked.The base implementation passes the call to the
nextinterceptor in the chain. When there is no next interceptor, the call fails fast with aNullPointerException: a chain built by the pool always ends in theProxyConnectionterminal, so reaching this case means a chain was hand-built without a terminal link. Interceptors that do not answer a call themselves continue it withsuper.invoke(proxy, method, args).- Specified by:
invokein interfaceInvocationHandler- Overrides:
invokein classJdbcInterceptor- Throws:
Throwable
-