Class StatementCache

All Implemented Interfaces:
InvocationHandler, StatementCacheMBean

public class StatementCache extends StatementDecoratorInterceptor implements StatementCacheMBean
Interceptor that caches PreparedStatement and/or CallableStatement instances on a connection.
  • Field Details

    • ALL_TYPES

      protected static final String[] ALL_TYPES
      All statement types (prepared and callable).
    • CALLABLE_TYPE

      protected static final String[] CALLABLE_TYPE
      Callable statement type only.
    • PREPARED_TYPE

      protected static final String[] PREPARED_TYPE
      Prepared statement type only.
    • NO_TYPE

      protected static final String[] NO_TYPE
      No statement types.
    • STATEMENT_CACHE_ATTR

      protected static final String STATEMENT_CACHE_ATTR
      Attribute key for storing the statement cache in connection attributes.
  • Constructor Details

    • StatementCache

      public StatementCache()
      Default constructor.
  • Method Details

    • isCachePrepared

      public boolean isCachePrepared()
      Description copied from interface: StatementCacheMBean
      Checks if prepared statements are being cached.
      Specified by:
      isCachePrepared in interface StatementCacheMBean
      Returns:
      true if prepared statement caching is enabled
    • isCacheCallable

      public boolean isCacheCallable()
      Description copied from interface: StatementCacheMBean
      Checks if callable statements are being cached.
      Specified by:
      isCacheCallable in interface StatementCacheMBean
      Returns:
      true if callable statement caching is enabled
    • getMaxCacheSize

      public int getMaxCacheSize()
      Description copied from interface: StatementCacheMBean
      Returns the maximum size of the statement cache.
      Specified by:
      getMaxCacheSize in interface StatementCacheMBean
      Returns:
      maximum cache size
    • getTypes

      public String[] getTypes()
      Returns the statement types being cached.
      Returns:
      array of statement type strings
    • getCacheSize

      public AtomicInteger getCacheSize()
      Description copied from interface: StatementCacheMBean
      Returns the current global cache size across all connections.
      Specified by:
      getCacheSize in interface StatementCacheMBean
      Returns:
      current cache size counter
    • setProperties

      public void setProperties(Map<String, PoolProperties.InterceptorProperty> properties)
      Description copied from class: JdbcInterceptor
      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 properties
      Overrides:
      setProperties in class JdbcInterceptor
      Parameters:
      properties - The properties
    • poolStarted

      public void poolStarted(ConnectionPool pool)
      Description copied from class: JdbcInterceptor
      This method is invoked by a connection pool when the pool is first started up, usually when the first connection is requested. Interceptor classes can override this method if they keep static variables or other tracking means around. This method is only invoked on a single instance of the interceptor, and not on every instance created.
      Overrides:
      poolStarted in class JdbcInterceptor
      Parameters:
      pool - - the pool that is being closed.
    • poolClosed

      public void poolClosed(ConnectionPool pool)
      Description copied from class: JdbcInterceptor
      This method is invoked by a connection pool when the pool is closed. Interceptor classes can override this method if they keep static variables or other tracking means around. This method is only invoked on a single instance of the interceptor, and not on every instance created.
      Overrides:
      poolClosed in class JdbcInterceptor
      Parameters:
      pool - - the pool that is being closed.
    • reset

      public void reset(ConnectionPool parent, PooledConnection con)
      Description copied from class: AbstractCreateStatementInterceptor
      no-op for this interceptor. no state is stored.
      Overrides:
      reset in class AbstractCreateStatementInterceptor
      Parameters:
      parent - - the connection pool owning the connection
      con - - the pooled connection
    • 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.
      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
    • closeStatement

      public void closeStatement(StatementCache.CachedStatement st)
      Closes the given cached statement by forcing it to close.
      Parameters:
      st - the cached statement to close
    • createDecorator

      protected Object createDecorator(Object proxy, Method method, Object[] args, Object statement, Constructor<?> constructor, String sql) throws InstantiationException, IllegalAccessException, InvocationTargetException
      Description copied from class: StatementDecoratorInterceptor
      Creates a proxy for a Statement.
      Overrides:
      createDecorator in class StatementDecoratorInterceptor
      Parameters:
      proxy - The proxy object on which the method that triggered the creation of the statement was called.
      method - The method that was called on the proxy
      args - The arguments passed as part of the method call to the proxy
      statement - The statement object that is to be proxied
      constructor - The constructor for the desired proxy
      sql - The sql of of the statement
      Returns:
      A new proxy for the Statement
      Throws:
      InstantiationException - Couldn't instantiate object
      IllegalAccessException - Inaccessible constructor
      InvocationTargetException - Exception thrown from constructor
    • 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 AbstractCreateStatementInterceptor
      Throws:
      Throwable
    • isCached

      public StatementCache.CachedStatement isCached(Method method, Object[] args)
      Checks if a statement for the given method and arguments is currently cached.
      Parameters:
      method - the SQL method (prepareStatement or prepareCall)
      args - the method arguments
      Returns:
      the cached statement, or null if not cached
    • cacheStatement

      public boolean cacheStatement(StatementCache.CachedStatement proxy)
      Caches the given statement proxy if there is room in the cache.
      Parameters:
      proxy - the statement proxy to cache
      Returns:
      true if the statement was cached, false otherwise
    • removeStatement

      public boolean removeStatement(StatementCache.CachedStatement proxy)
      Removes the given statement from the cache.
      Parameters:
      proxy - the statement proxy to remove
      Returns:
      true if the statement was removed, false otherwise
    • getCache

      protected ConcurrentHashMap<org.apache.tomcat.jdbc.pool.interceptor.StatementCache.CacheKey, StatementCache.CachedStatement> getCache()
      Returns the statement cache for the current pooled connection.
      Returns:
      the cache map, or null if the connection has been closed
    • getCacheSizePerConnection

      public int getCacheSizePerConnection()
      Description copied from interface: StatementCacheMBean
      Returns the cache size for the current connection.
      Specified by:
      getCacheSizePerConnection in interface StatementCacheMBean
      Returns:
      number of cached statements for this connection
    • createCacheKey

      protected org.apache.tomcat.jdbc.pool.interceptor.StatementCache.CacheKey createCacheKey(Method method, Object[] args)
      Creates a cache key from the given method and arguments.
      Parameters:
      method - the SQL method
      args - the method arguments
      Returns:
      the cache key
    • createCacheKey

      protected org.apache.tomcat.jdbc.pool.interceptor.StatementCache.CacheKey createCacheKey(String methodName, Object[] args)
      Creates a cache key from the given method name and arguments.
      Parameters:
      methodName - the SQL method name
      args - the method arguments
      Returns:
      the cache key, or null if the method name doesn't match a cached type