Class ConnectionState

java.lang.Object
org.apache.tomcat.jdbc.pool.JdbcInterceptor
org.apache.tomcat.jdbc.pool.interceptor.ConnectionState
All Implemented Interfaces:
InvocationHandler

public class ConnectionState extends JdbcInterceptor
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 Details

    • readState

      protected final String[] readState
      Method names for reading connection state.
    • writeState

      protected final String[] writeState
      Method names for writing connection state.
    • autoCommit

      protected Boolean autoCommit
      Cached auto-commit state of the connection.
    • transactionIsolation

      protected Integer transactionIsolation
      Cached transaction isolation level of the connection.
    • readOnly

      protected Boolean readOnly
      Cached read-only state of the connection.
    • catalog

      protected String catalog
      Cached catalog (database) name of the connection.
  • Constructor Details

    • ConnectionState

      public ConnectionState()
      Creates a new ConnectionState interceptor instance.
  • Method Details

    • 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. 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:
      reset in class JdbcInterceptor
      Parameters:
      parent - the pool the connection belongs to, or null when the interceptor chain is being torn down
      con - the pooled connection being set up for its next borrower
    • disconnected

      public void disconnected(ConnectionPool parent, PooledConnection con, boolean finalizing)
      Description copied from class: JdbcInterceptor
      Called when Connection.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:
      disconnected in class JdbcInterceptor
      Parameters:
      parent - - the connection pool that this connection belongs to
      con - - the pooled connection that holds this connection
      finalizing - - if this connection is finalizing. True means that the pooled connection will not reconnect the underlying connection
    • invoke

      public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
      Description copied from class: JdbcInterceptor
      Gets invoked each time an operation on Connection is invoked.
      Specified by:
      invoke in interface InvocationHandler
      Overrides:
      invoke in class JdbcInterceptor
      Throws:
      Throwable