Class StatementCache
- All Implemented Interfaces:
InvocationHandler, StatementCacheMBean
PreparedStatement and/or CallableStatement instances on a connection.
A cache hit is answered by this interceptor alone: the rest of the interceptor chain is not invoked for it, and a
statement recycled into the cache is reset to driver defaults (its query timeout goes back to zero). Interceptors
listed after this one in jdbcInterceptors therefore do not see reused statements - relevant to any interceptor whose createStatement
hook performs an action rather than only wrapping the statement: QueryTimeoutInterceptor must be listed
before this one to keep its timeout on cached statements, while wrapping-only interceptors are unaffected: their
wrapper is carried inside the cached proxy, and the cache binds a cached statement to the physical session the
prepare ran on rather than to whatever connection a wrapper beneath it reports for the statement.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected classProxy for a cached prepared statement.Nested classes/interfaces inherited from class StatementDecoratorInterceptor
StatementDecoratorInterceptor.ResultSetProxy, StatementDecoratorInterceptor.StatementProxy<T>Modifier and TypeClassDescriptionprotected static classInvocation handler that proxies ResultSet instances.protected classClass to measure query execute time. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final String[]All statement types (prepared and callable).protected static final String[]Callable statement type only.protected static final String[]No statement types.protected static final String[]Prepared statement type only.protected static final StringAttribute key for storing the statement cache in connection attributes.Fields inherited from class StatementDecoratorInterceptor
EXECUTE_QUERY, GET_GENERATED_KEYS, GET_RESULTSET, RESULTSET_TYPES, resultSetConstructorModifier and TypeFieldDescriptionprotected static final StringMethod name for executeQuery.protected static final StringMethod name for getGeneratedKeys.protected static final StringMethod name for getResultSet.protected static final String[]Method names that return a ResultSet.protected static Constructor<?> the constructor to create the resultSet proxiesFields inherited from class AbstractCreateStatementInterceptor
constructors, CREATE_STATEMENT, CREATE_STATEMENT_IDX, EXECUTE, EXECUTE_BATCH, EXECUTE_LARGE_BATCH, EXECUTE_LARGE_UPDATE, EXECUTE_TYPES, EXECUTE_UPDATE, GET_STATEMENT, PREPARE_CALL, PREPARE_CALL_IDX, PREPARE_STATEMENT, PREPARE_STATEMENT_IDX, RESULTSET_IDX, STATEMENT_TYPE_COUNT, STATEMENT_TYPESModifier and TypeFieldDescriptionprotected static final Constructor<?>[]the constructors that are used to create statement proxies.protected static final Stringprotected static final intprotected static final Stringprotected static final Stringprotected static final Stringprotected static final StringStatement.executeLargeUpdate(String)and its overloads, andPreparedStatement.executeLargeUpdate()protected static final String[]Array of execute type method namesprotected static final Stringprotected static final Stringprotected static final Stringprotected static final intprotected static final Stringprotected static final intprotected static final intprotected static final intCount of statement types inAbstractCreateStatementInterceptor.STATEMENT_TYPESprotected static final String[]Array of statement type method namesFields 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 TypeMethodDescriptionbooleanCaches the given statement proxy if there is room in the cache.voidCloses the given cached statement by forcing it to close.protected org.apache.tomcat.jdbc.pool.interceptor.StatementCache.CacheKeycreateCacheKey(Method method, Object[] args) Creates a cache key from the given method and arguments.protected org.apache.tomcat.jdbc.pool.interceptor.StatementCache.CacheKeycreateCacheKey(String methodName, Object[] args) Creates a cache key from the given method name and arguments.protected ObjectcreateDecorator(Object proxy, Method method, Object[] args, Object statement, Constructor<?> constructor, 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 ConcurrentHashMap<org.apache.tomcat.jdbc.pool.interceptor.StatementCache.CacheKey, StatementCache.CachedStatement> getCache()Returns the statement cache for the current pooled connection.intReturns the number of statements currently held in the statement cache, counted across all connections that belong to the same connection pool - the value themaxlimit is compared against.intReturns the cache size for the current connection.intReturns the maximum size of the statement cache.String[]getTypes()Returns the statement types being cached.Gets invoked each time an operation onConnectionis invoked.booleanChecks if callable statements are being cached.Checks if a statement for the given method and arguments is currently cached.booleanChecks 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.booleanRemoves the given statement from the cache.voidreset(ConnectionPool parent, PooledConnection con) no-op for this interceptor. no state is stored.voidsetProperties(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 propertiesMethods inherited from class StatementDecoratorInterceptor
closeInvoked, createStatement, getResultSetConstructor, isExecuteQuery, isExecuteQuery, isResultSetModifier and TypeMethodDescriptionvoidMethod invoked when the operationConnection.close()is invoked.createStatement(Object proxy, Method method, Object[] args, Object statement, long time) Creates a statement interceptor to monitor query response timesprotected Constructor<?> Gets the constructor for creating ResultSet proxies.protected booleanisExecuteQuery(Method method) Checks if the given method is executeQuery.protected booleanisExecuteQuery(String methodName) Checks if the given method name is executeQuery.protected booleanisResultSet(Method method, boolean process) Checks if the given method returns a ResultSet.Methods inherited from class AbstractCreateStatementInterceptor
getConstructor, isExecute, isStatement, processModifier and TypeMethodDescriptionprotected Constructor<?> getConstructor(int idx, Class<?> clazz) Creates a constructor for a proxy class, if one doesn't already exist.protected booleanReturns true if the method that is being invoked matches one of the execute types.protected booleanisStatement(Method method, boolean process) Returns true if the method that is being invoked matches one of the statement types.protected booleanReturns true if the method that is being invoked matches one of the method names passed inMethods inherited from class JdbcInterceptor
compare, compare, getNext, getProperties, isUseEquals, setNext, 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.voidsetNext(JdbcInterceptor next) configures the next interceptor in the chainvoidsetUseEquals(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
-
ALL_TYPES
All statement types (prepared and callable). -
CALLABLE_TYPE
Callable statement type only. -
PREPARED_TYPE
Prepared statement type only. -
NO_TYPE
No statement types. -
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: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
Returns the statement types being cached.- Returns:
- array of statement type strings
-
getCacheSize
public int getCacheSize()Description copied from interface:StatementCacheMBeanReturns the number of statements currently held in the statement cache, counted across all connections that belong to the same connection pool - the value themaxlimit is compared against. The number is a snapshot of the pool's accounting at the moment of the read; it is not a handle that can be used to change that accounting.- Specified by:
getCacheSizein interfaceStatementCacheMBean- Returns:
- the number of cached statements, or
-1if this interceptor instance is not bound to a pooled connection
-
setProperties
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
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
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
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
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
-
closeStatement
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: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:
InstantiationException- Couldn't instantiate objectIllegalAccessException- Inaccessible constructorInvocationTargetException- Exception thrown from constructor
-
invoke
Description copied from class:JdbcInterceptorGets invoked each time an operation onConnectionis invoked.- Specified by:
invokein interfaceInvocationHandler- Overrides:
invokein classAbstractCreateStatementInterceptor- Throws:
Throwable
-
isCached
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
Caches the given statement proxy if there is room in the cache.The insertion is atomic: of two statements closed concurrently under one key exactly one is parked, and the other is refused as a duplicate - a park can never overwrite, and so can never orphan, a statement another close has already parked. A proxy that is refused was never in the map, and the caller that receives
falsemust close the statement it offered.- Parameters:
proxy- the statement proxy to cache- Returns:
- true if the statement was cached, false otherwise
-
removeStatement
Removes the given statement from the cache. The removal is identity-aware: it only takes effect while the entry under the statement's key still holds this very proxy, so a proxy that was never parked - or is no longer the cached statement - cannot evict a different statement parked under an equal key.- 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:StatementCacheMBeanReturns the cache size for the current connection.- Specified by:
getCacheSizePerConnectionin interfaceStatementCacheMBean- Returns:
- number of cached statements for this connection
-
createCacheKey
-
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 nameargs- the method arguments- Returns:
- the cache key, or null if the method name doesn't match a cached type
-