Class ConnectionState
java.lang.Object
org.apache.tomcat.jdbc.pool.JdbcInterceptor
org.apache.tomcat.jdbc.pool.interceptor.ConnectionState
- All Implemented Interfaces:
InvocationHandler
Interceptor that keep track of connection state to avoid roundtrips to the database. The
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.-
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.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_VALModifier and TypeFieldDescriptionstatic final StringConnection.abort(java.util.concurrent.Executor)method namestatic final StringConnection.close()method namestatic final Stringstatic final StringPooledConnection.getConnection()method namestatic final Stringstatic final StringConnection.isClosed()method namestatic final StringConnection.isValid(int)method namestatic final StringWrapper.isWrapperFor(Class)method nameprotected Map<String, PoolProperties.InterceptorProperty> Properties for this interceptor.static final StringObject.toString()method namestatic final StringWrapper.unwrap(Class)method name -
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, setUseEqualsModifier and TypeMethodDescriptionbooleanCompares a method name (String) to a method (Method)JdbcInterceptor.compare(String,String)Uses reference comparison unless the useEquals property is set to truebooleanPerforms a string comparison, using references unless the useEquals property is set to true.getNext()Returns the next interceptor in the chainReturns the properties configured for this interceptorbooleanTrue if the compare method uses the Object.equals(Object) method, false if comparison is done on a reference level.voidpoolClosed(ConnectionPool pool) This method is invoked by a connection pool when the pool is closed.voidpoolStarted(ConnectionPool pool) This method is invoked by a connection pool when the pool is first started up, usually when the first connection is requested.voidsetNext(JdbcInterceptor next) configures the next interceptor in the chainvoidsetProperties(Map<String, PoolProperties.InterceptorProperty> properties) Called during the creation of an interceptor The properties can be set during the configuration of an interceptor Override this method to perform type casts between string values and object propertiesvoidsetUseEquals(boolean useEquals) Set to true if string comparisons (for theJdbcInterceptor.compare(String, Method)andJdbcInterceptor.compare(String, String)methods) should use the Object.equals(Object) method The default is false
-
Field Details
-
readState
Method names for reading connection state. -
writeState
Method names for writing connection state. -
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. If the previous borrower returned the connection while still in a transaction, that transaction is terminated first: the isolation level and the read only state are transaction scoped on many drivers, and an open transaction would otherwise absorb the restores below (or, when auto-commit is restored, silently commit the previous borrower's uncommitted work). If terminating that transaction fails, the borrow fails and the pool retires the connection. So does a failed restore of the read-only, auto-commit or catalog state: a connection known to sit in a state other than the configured default must not reach the next borrower. A transaction isolation level the driver refuses is the exception: the connection stays in service, so the pool never strands borrowers over that setting. The refusal is logged once, at refusal time, and that level then stops counting as a pending restore on that session - later borrows neither retry it nor log it again nor terminate a pending transaction on its behalf. The record covers the refused level only: the pool reads the configured level on every borrow, so a level the session has not refused yet - one configured after the refusal, for instance - is attempted and applied as usual, and a replaced session retries even the same level. If the refused level was the only pending restore, the next borrower then inherits an open transaction left by the previous one, exactly as it does for a pool that configures no defaults at all. Nothing is terminated when there is nothing to restore - no default is configured, the cache already shows every configured default in place, or the only default that would need restoring is one this session refused - so a pool that uses this interceptor only to cache the state does not pay for the termination.- 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.- 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.- Specified by:
invokein interfaceInvocationHandler- Overrides:
invokein classJdbcInterceptor- Throws:
Throwable
-