Class StatementCache
- java.lang.Object
-
- org.apache.tomcat.jdbc.pool.JdbcInterceptor
-
- org.apache.tomcat.jdbc.pool.interceptor.AbstractCreateStatementInterceptor
-
- org.apache.tomcat.jdbc.pool.interceptor.StatementDecoratorInterceptor
-
- org.apache.tomcat.jdbc.pool.interceptor.StatementCache
-
- All Implemented Interfaces:
java.lang.reflect.InvocationHandler,StatementCacheMBean
public class StatementCache extends StatementDecoratorInterceptor implements StatementCacheMBean
Interceptor that cachesPreparedStatementand/orCallableStatementinstances on a connection.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected classStatementCache.CachedStatementProxy for a cached prepared statement.-
Nested classes/interfaces inherited from class org.apache.tomcat.jdbc.pool.interceptor.StatementDecoratorInterceptor
StatementDecoratorInterceptor.ResultSetProxy, StatementDecoratorInterceptor.StatementProxy<T extends java.sql.Statement>
-
-
Field Summary
Fields Modifier and Type Field Description protected static java.lang.String[]ALL_TYPESAll statement types (prepared and callable).protected static java.lang.String[]CALLABLE_TYPECallable statement type only.protected static java.lang.String[]NO_TYPENo statement types.protected static java.lang.String[]PREPARED_TYPEPrepared statement type only.protected static java.lang.StringSTATEMENT_CACHE_ATTRAttribute key for storing the statement cache in connection attributes.-
Fields inherited from class org.apache.tomcat.jdbc.pool.interceptor.StatementDecoratorInterceptor
EXECUTE_QUERY, GET_GENERATED_KEYS, GET_RESULTSET, RESULTSET_TYPES, resultSetConstructor
-
Fields inherited from class org.apache.tomcat.jdbc.pool.interceptor.AbstractCreateStatementInterceptor
constructors, CREATE_STATEMENT, CREATE_STATEMENT_IDX, EXECUTE, EXECUTE_BATCH, EXECUTE_TYPES, EXECUTE_UPDATE, GET_STATEMENT, PREPARE_CALL, PREPARE_CALL_IDX, PREPARE_STATEMENT, PREPARE_STATEMENT_IDX, RESULTSET_IDX, STATEMENT_TYPE_COUNT, STATEMENT_TYPES
-
Fields inherited from class org.apache.tomcat.jdbc.pool.JdbcInterceptor
CLOSE_VAL, EQUALS_VAL, GETCONNECTION_VAL, HASHCODE_VAL, ISCLOSED_VAL, ISVALID_VAL, ISWRAPPERFOR_VAL, properties, TOSTRING_VAL, UNWRAP_VAL
-
-
Constructor Summary
Constructors Constructor Description StatementCache()Default constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleancacheStatement(StatementCache.CachedStatement proxy)Caches the given statement proxy if there is room in the cache.voidcloseStatement(StatementCache.CachedStatement st)Closes the given cached statement by forcing it to close.protected org.apache.tomcat.jdbc.pool.interceptor.StatementCache.CacheKeycreateCacheKey(java.lang.reflect.Method method, java.lang.Object[] args)Creates a cache key from the given method and arguments.protected org.apache.tomcat.jdbc.pool.interceptor.StatementCache.CacheKeycreateCacheKey(java.lang.String methodName, java.lang.Object[] args)Creates a cache key from the given method name and arguments.protected java.lang.ObjectcreateDecorator(java.lang.Object proxy, java.lang.reflect.Method method, java.lang.Object[] args, java.lang.Object statement, java.lang.reflect.Constructor<?> constructor, java.lang.String sql)Creates a proxy for a Statement.voiddisconnected(ConnectionPool parent, PooledConnection con, boolean finalizing)Called whenConnection.close()is called on the underlying connection.protected java.util.concurrent.ConcurrentHashMap<org.apache.tomcat.jdbc.pool.interceptor.StatementCache.CacheKey,StatementCache.CachedStatement>getCache()Returns the statement cache for the current pooled connection.java.util.concurrent.atomic.AtomicIntegergetCacheSize()Returns the current global cache size across all connections.intgetCacheSizePerConnection()Returns the cache size for the current connection.intgetMaxCacheSize()Returns the maximum size of the statement cache.java.lang.String[]getTypes()Returns the statement types being cached.java.lang.Objectinvoke(java.lang.Object proxy, java.lang.reflect.Method method, java.lang.Object[] args)Gets invoked each time an operation onConnectionis invoked.booleanisCacheCallable()Checks if callable statements are being cached.StatementCache.CachedStatementisCached(java.lang.reflect.Method method, java.lang.Object[] args)Checks if a statement for the given method and arguments is currently cached.booleanisCachePrepared()Checks if prepared statements are being cached.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.booleanremoveStatement(StatementCache.CachedStatement proxy)Removes the given statement from the cache.voidreset(ConnectionPool parent, PooledConnection con)no-op for this interceptor. no state is stored.voidsetProperties(java.util.Map<java.lang.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 properties-
Methods inherited from class org.apache.tomcat.jdbc.pool.interceptor.StatementDecoratorInterceptor
closeInvoked, createStatement, getResultSetConstructor, isExecuteQuery, isExecuteQuery, isResultSet
-
Methods inherited from class org.apache.tomcat.jdbc.pool.interceptor.AbstractCreateStatementInterceptor
getConstructor, isExecute, isStatement, process
-
Methods inherited from class org.apache.tomcat.jdbc.pool.JdbcInterceptor
compare, compare, getNext, getProperties, isUseEquals, setNext, setUseEquals
-
-
-
-
Field Detail
-
ALL_TYPES
protected static final java.lang.String[] ALL_TYPES
All statement types (prepared and callable).
-
CALLABLE_TYPE
protected static final java.lang.String[] CALLABLE_TYPE
Callable statement type only.
-
PREPARED_TYPE
protected static final java.lang.String[] PREPARED_TYPE
Prepared statement type only.
-
NO_TYPE
protected static final java.lang.String[] NO_TYPE
No statement types.
-
STATEMENT_CACHE_ATTR
protected static final java.lang.String STATEMENT_CACHE_ATTR
Attribute key for storing the statement cache in connection attributes.
-
-
Method Detail
-
isCachePrepared
public boolean isCachePrepared()
Description copied from interface:StatementCacheMBeanChecks if prepared statements are being cached.- Specified by:
isCachePreparedin interfaceStatementCacheMBean- Returns:
- true if prepared statement caching is enabled
-
isCacheCallable
public boolean isCacheCallable()
Description copied from interface:StatementCacheMBeanChecks if callable statements are being cached.- Specified by:
isCacheCallablein interfaceStatementCacheMBean- Returns:
- true if callable statement caching is enabled
-
getMaxCacheSize
public int getMaxCacheSize()
Description copied from interface:StatementCacheMBeanReturns the maximum size of the statement cache.- Specified by:
getMaxCacheSizein interfaceStatementCacheMBean- Returns:
- maximum cache size
-
getTypes
public java.lang.String[] getTypes()
Returns the statement types being cached.- Returns:
- array of statement type strings
-
getCacheSize
public java.util.concurrent.atomic.AtomicInteger getCacheSize()
Description copied from interface:StatementCacheMBeanReturns the current global cache size across all connections.- Specified by:
getCacheSizein interfaceStatementCacheMBean- Returns:
- current cache size counter
-
setProperties
public void setProperties(java.util.Map<java.lang.String,PoolProperties.InterceptorProperty> properties)
Description copied from class:JdbcInterceptorCalled 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:
setPropertiesin classJdbcInterceptor- Parameters:
properties- The properties
-
poolStarted
public void poolStarted(ConnectionPool pool)
Description copied from class:JdbcInterceptorThis 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:
poolStartedin classJdbcInterceptor- Parameters:
pool- - the pool that is being closed.
-
poolClosed
public void poolClosed(ConnectionPool pool)
Description copied from class:JdbcInterceptorThis 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:
poolClosedin classJdbcInterceptor- Parameters:
pool- - the pool that is being closed.
-
reset
public void reset(ConnectionPool parent, PooledConnection con)
Description copied from class:AbstractCreateStatementInterceptorno-op for this interceptor. no state is stored.- Overrides:
resetin classAbstractCreateStatementInterceptor- Parameters:
parent- - the connection pool owning the connectioncon- - the pooled connection
-
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.- 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
-
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 java.lang.Object createDecorator(java.lang.Object proxy, java.lang.reflect.Method method, java.lang.Object[] args, java.lang.Object statement, java.lang.reflect.Constructor<?> constructor, java.lang.String sql) throws java.lang.InstantiationException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetExceptionDescription copied from class:StatementDecoratorInterceptorCreates a proxy for a Statement.- Overrides:
createDecoratorin classStatementDecoratorInterceptor- 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 proxyargs- The arguments passed as part of the method call to the proxystatement- The statement object that is to be proxiedconstructor- The constructor for the desired proxysql- The sql of of the statement- Returns:
- A new proxy for the Statement
- Throws:
java.lang.InstantiationException- Couldn't instantiate objectjava.lang.IllegalAccessException- Inaccessible constructorjava.lang.reflect.InvocationTargetException- Exception thrown from constructor
-
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.- Specified by:
invokein interfacejava.lang.reflect.InvocationHandler- Overrides:
invokein classAbstractCreateStatementInterceptor- Throws:
java.lang.Throwable
-
isCached
public StatementCache.CachedStatement isCached(java.lang.reflect.Method method, java.lang.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 java.util.concurrent.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:StatementCacheMBeanReturns the cache size for the current connection.- Specified by:
getCacheSizePerConnectionin interfaceStatementCacheMBean- Returns:
- number of cached statements for this connection
-
createCacheKey
protected org.apache.tomcat.jdbc.pool.interceptor.StatementCache.CacheKey createCacheKey(java.lang.reflect.Method method, java.lang.Object[] args)Creates a cache key from the given method and arguments.- Parameters:
method- the SQL methodargs- the method arguments- Returns:
- the cache key
-
createCacheKey
protected org.apache.tomcat.jdbc.pool.interceptor.StatementCache.CacheKey createCacheKey(java.lang.String methodName, java.lang.Object[] args)Creates a cache key from the given method name and arguments.- Parameters:
methodName- the SQL method nameargs- the method arguments- Returns:
- the cache key, or null if the method name doesn't match a cached type
-
-