Class ConnectionState
- java.lang.Object
-
- org.apache.tomcat.jdbc.pool.JdbcInterceptor
-
- org.apache.tomcat.jdbc.pool.interceptor.ConnectionState
-
- All Implemented Interfaces:
java.lang.reflect.InvocationHandler
public class ConnectionState extends JdbcInterceptor
Interceptor that keeps track of connection state to avoid roundtrips to the database. TheConnectionPoolis optimized to do as little work as possible. The pool itself doesn't remember settings likeConnection.setAutoCommit(boolean),Connection.setReadOnly(boolean),Connection.setCatalog(String)orConnection.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,defaultTransactionIsolationanddefaultCatalog:PooledConnection.connect()no longer applies them at connection creation, andreset(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
Fields Modifier and Type Field Description protected java.lang.BooleanautoCommitCached auto-commit state of the connection.protected java.lang.StringcatalogCached catalog (database) name of the connection.protected java.lang.BooleanreadOnlyCached read-only state of the connection.protected java.lang.String[]readStateMethod names for reading connection state.protected java.lang.IntegertransactionIsolationCached transaction isolation level of the connection.protected java.lang.String[]writeStateMethod names for writing connection state, indexed asreadState.-
Fields inherited from class org.apache.tomcat.jdbc.pool.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 Constructor Description ConnectionState()Creates a new ConnectionState interceptor instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voiddisconnected(ConnectionPool parent, PooledConnection con, boolean finalizing)Called whenConnection.close()is called on the underlying connection.java.lang.Objectinvoke(java.lang.Object proxy, java.lang.reflect.Method method, java.lang.Object[] args)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 org.apache.tomcat.jdbc.pool.JdbcInterceptor
compare, compare, getNext, getProperties, isUseEquals, poolClosed, poolStarted, setNext, setProperties, setUseEquals, unregisterMBean
-
-
-
-
Field Detail
-
readState
protected final java.lang.String[] 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(java.lang.Object, java.lang.reflect.Method, java.lang.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(java.lang.Object, java.lang.reflect.Method, java.lang.Object[])- at once.
-
writeState
protected final java.lang.String[] writeState
Method names for writing connection state, indexed asreadState.
-
autoCommit
protected java.lang.Boolean autoCommit
Cached auto-commit state of the connection.
-
transactionIsolation
protected java.lang.Integer transactionIsolation
Cached transaction isolation level of the connection.
-
readOnly
protected java.lang.Boolean readOnly
Cached read-only state of the connection.
-
catalog
protected java.lang.String catalog
Cached catalog (database) name of the connection.
-
-
Method Detail
-
reset
public void reset(ConnectionPool parent, PooledConnection con)
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(org.apache.tomcat.jdbc.pool.PooledConnection, java.sql.SQLException, org.apache.tomcat.jdbc.pool.interceptor.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
public void disconnected(ConnectionPool parent, PooledConnection con, boolean finalizing)
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
public java.lang.Object invoke(java.lang.Object proxy, java.lang.reflect.Method method, java.lang.Object[] args) throws java.lang.ThrowableDescription 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 interfacejava.lang.reflect.InvocationHandler- Overrides:
invokein classJdbcInterceptor- Throws:
java.lang.Throwable
-
-