Class QueryTimeoutInterceptor

All Implemented Interfaces:
InvocationHandler

public class QueryTimeoutInterceptor extends AbstractCreateStatementInterceptor
Interceptor that sets a query timeout on every created Statement.

The timeout is applied by the createStatement hook, which only runs for statement-creation calls that pass through this interceptor. When combined with StatementCache, this interceptor must therefore be listed before StatementCache in the jdbcInterceptors configuration. A statement cache hit is served by the cache without invoking the rest of the chain, and a statement recycled into the cache has its query timeout reset to zero, so an interceptor listed after the cache never sees the reuse and the cached statement executes with no timeout. Listed before the cache, the hook runs for every creation request, including the ones the cache serves, and the timeout is re-applied to the reused statement.

Whether a configured timeout actually interrupts a query is up to the driver. A driver that refuses Statement.setQueryTimeout(int) does not stop the statement from being handed out - the pool cannot enforce a timeout itself, so such a statement runs unbounded - and the refusal is reported once per physical connection rather than once per statement, so that the log says what is not in effect without repeating itself for every statement that connection creates.

  • Constructor Details

    • QueryTimeoutInterceptor

      public QueryTimeoutInterceptor()
      Default constructor.
  • Method Details

    • setProperties

      public void setProperties(Map<String, PoolProperties.InterceptorProperty> properties)
      Initializes the queryTimeout property from the provided configuration map. A value that is not an integer is not applied and is warned about: the timeout keeps the value it has - for a fresh instance the one-second default - so a misconfigured pool behaves as the default and the warning says so as the bad value is configured. The pool configures an interceptor instance once at pool start, once per physical connection chain, and once when the pool stops, so the report shares the per-connection reporting volume of the driver refusal below.
      Overrides:
      setProperties in class JdbcInterceptor
      Parameters:
      properties - map of interceptor properties
    • createStatement

      public Object createStatement(Object proxy, Method method, Object[] args, Object statement, long time)
      Sets the query timeout on the created statement if it is a Statement and the timeout value is positive.
      Specified by:
      createStatement in class AbstractCreateStatementInterceptor
      Parameters:
      proxy - the actual proxy object
      method - the method that was called
      args - the arguments to the method
      statement - the statement that the underlying connection created
      time - elapsed time in milliseconds
      Returns:
      the (possibly wrapped) statement object
    • disconnected

      public void disconnected(ConnectionPool parent, PooledConnection con, boolean finalizing)
      Forgets the reported refusal, as the physical connection this interceptor is attached to is gone: the session that replaces it gets its own refusal recorded and its own warning, the way it gets its own driver to answer the call.
      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
    • closeInvoked

      public void closeInvoked()
      No-op; the refusal record belongs to the physical connection, not to the borrow, so ending a borrow has nothing here to clean up.
      Specified by:
      closeInvoked in class AbstractCreateStatementInterceptor