Class SlowQueryReport
- All Implemented Interfaces:
InvocationHandler
- Direct Known Subclasses:
SlowQueryReportJmx
The statistics are published under the pool name (see getPoolStats(String)), registered when a pool
starts and released when the last live pool registered under that name closes. Pools configured with the same name -
possible with jmxEnabled=false, which removes the JMX ObjectName clash that would otherwise catch it -
therefore share one statistics map; closing one of them never takes the shared statistics away from the others.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static classThe query statistics published under one pool name, together with the live pools registered for that name.static classHolds timing and invocation statistics for a single SQL query.static classCompares QueryStats by their lastInvocation value.Nested classes/interfaces inherited from class AbstractQueryReport
AbstractQueryReport.StatementProxyModifier and TypeClassDescriptionprotected classClass to measure query execute time -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected booleanWhether thepoolClosed(ConnectionPool)delivered to this instance deregistered the closing pool as the last live pool under its name, meaning everything else registered for that name - the interceptor's MBean and broadcaster, for instance - can and must now be released with it.protected booleanFlag to enable disable logging of failed queriesprotected booleanFlag to enable disable logging of slow queriesprotected intMaximum number of queries we will be storing statistics for.protected static final ConcurrentHashMap<String, SlowQueryReport.PoolStatistics> we will be keeping track of query stats on a per pool basis, keyed by pool name; pools registered under one name share the statisticsprotected ConcurrentHashMap<String, SlowQueryReport.QueryStats> the queries that are used for this interceptor.protected final Comparator<SlowQueryReport.QueryStats> Sort QueryStats by last invocation timeFields inherited from class AbstractQueryReport
thresholdFields inherited from class AbstractCreateStatementInterceptor
constructors, CREATE_STATEMENT, CREATE_STATEMENT_IDX, EXECUTE, EXECUTE_BATCH, EXECUTE_LARGE_BATCH, EXECUTE_LARGE_UPDATE, EXECUTE_QUERY, EXECUTE_TYPES, EXECUTE_UPDATE, GET_GENERATED_KEYS, GET_RESULTSET, 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 Stringprotected static final String[]Array of execute type method namesprotected static final Stringprotected static final Stringprotected 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 TypeMethodDescriptionvoidinvoked when the connection receives the close request Not used for now.getPoolStats(String poolname) Returns the query stats for a given poolprotected SlowQueryReport.QueryStatsgetQueryStats(String sql) Retrieves or creates a QueryStats entry for the given SQL string.booleanReturns whether failed query logging is enabled.booleanReturns whether slow query logging is enabled.voidpoolClosed(ConnectionPool pool) Releases the query statistics map for the given connection pool, unless another live pool is still registered under its name.voidpoolStarted(ConnectionPool pool) Initializes the query statistics map for the given connection pool.voidprepareCall(String sql, long time) Tracks the preparation time for a callable statement.voidprepareStatement(String sql, long time) Tracks the preparation time for a prepared statement.protected static booleanDeregisters a closed pool from the live pools that the per-name statistics are held for, and releases the statistics once no live pool is registered under the name any more.protected voidEvicts the least recently invoked query statistics until the map is back withinmaxQueriesentries.protected StringreportFailedQuery(String query, Object[] args, String name, long start, long delta, Throwable t) Invoked when a query execution, a call to an execute method as recognised byAbstractCreateStatementInterceptor.isExecute(Method, boolean), failed.protected StringreportQuery(String query, Object[] args, String name, long start, long delta) Invoked when a query execution, a call to an execute method as recognised byAbstractCreateStatementInterceptor.isExecute(Method, boolean), succeeded and was within the timing thresholdprotected StringreportSlowQuery(String query, Object[] args, String name, long start, long delta) Invoked when a query execution, a call to an execute method as recognised byAbstractCreateStatementInterceptor.isExecute(Method, boolean), succeeded and was exceeded the timing thresholdvoidreset(ConnectionPool parent, PooledConnection con) Resets the query statistics reference when a connection is reset.voidsetLogFailed(boolean logFailed) Enables or disables failed query logging.voidsetLogSlow(boolean logSlow) Enables or disables slow query logging.voidsetMaxQueries(int maxQueries) Sets the maximum number of queries to track statistics for.voidsetProperties(Map<String, PoolProperties.InterceptorProperty> properties) Configures this interceptor from the given properties map, supporting threshold, maxQueries, logSlow, and logFailed settings.Methods inherited from class AbstractQueryReport
createStatement, getThreshold, setThresholdModifier and TypeMethodDescriptioncreateStatement(Object proxy, Method method, Object[] args, Object statement, long time) Creates a statement interceptor to monitor query response timeslongreturns the query measure threshold.voidsetThreshold(long threshold) Sets the query measurement threshold.Methods inherited from class AbstractCreateStatementInterceptor
getConstructor, invoke, isExecute, isStatement, processModifier and TypeMethodDescriptionprotected Constructor<?> getConstructor(int idx, Class<?> clazz) Creates a constructor for a proxy class, if one doesn't already exist.Gets invoked each time an operation onConnectionis invoked.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, disconnected, 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.voiddisconnected(ConnectionPool parent, PooledConnection con, boolean finalizing) Called whenConnection.close()is called on the underlying connection.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
-
perPoolStats
we will be keeping track of query stats on a per pool basis, keyed by pool name; pools registered under one name share the statistics -
queries
the queries that are used for this interceptor. -
maxQueries
protected int maxQueriesMaximum number of queries we will be storing statistics for. Eviction keeps the number of stored queries near this amount; while queries are being executed concurrently it can briefly be exceeded. This sizes the statistics store only: the slow and failed query logging is configured bysetLogSlow(boolean)andsetLogFailed(boolean)independently of this value. -
logSlow
protected boolean logSlowFlag to enable disable logging of slow queries -
logFailed
protected boolean logFailedFlag to enable disable logging of failed queries -
queryStatsComparator
Sort QueryStats by last invocation time -
lastPoolOfNameClosed
protected boolean lastPoolOfNameClosedWhether thepoolClosed(ConnectionPool)delivered to this instance deregistered the closing pool as the last live pool under its name, meaning everything else registered for that name - the interceptor's MBean and broadcaster, for instance - can and must now be released with it. The decidingreleaseStatistics(ConnectionPool)call is atomic, so across concurrent closes of same-named pools and across the interceptor definitions of one close exactly one caller ever seestrue.
-
-
Constructor Details
-
SlowQueryReport
public SlowQueryReport()Creates a slow query report interceptor
-
-
Method Details
-
getPoolStats
Returns the query stats for a given pool- Parameters:
poolname- - the name of the pool we want to retrieve stats for- Returns:
- a hash map containing the statistics of the monitored queries, kept near
maxQueriesentries by eviction, ornullif no live pool is registered under that name. Pools registered under one name share the map, and it is published until the last of them has closed
-
setMaxQueries
public void setMaxQueries(int maxQueries) Sets the maximum number of queries to track statistics for.- Parameters:
maxQueries- the maximum number of queries
-
reportFailedQuery
protected String reportFailedQuery(String query, Object[] args, String name, long start, long delta, Throwable t) Description copied from class:AbstractQueryReportInvoked when a query execution, a call to an execute method as recognised byAbstractCreateStatementInterceptor.isExecute(Method, boolean), failed.- Overrides:
reportFailedQueryin classAbstractQueryReport- Parameters:
query- the query that was executed and failedargs- the arguments to the executionname- the name of the method used to executeAbstractCreateStatementInterceptor.isExecute(Method, boolean)start- the wall clock time the query execution starteddelta- the time the execution took before it failed, measured with the monotonic clockt- the exception that happened- Returns:
- - the SQL that was executed or the string "batch" if it was a batch execution, or
nullif the invocation carries no SQL string
-
reportQuery
Description copied from class:AbstractQueryReportInvoked when a query execution, a call to an execute method as recognised byAbstractCreateStatementInterceptor.isExecute(Method, boolean), succeeded and was within the timing threshold- Overrides:
reportQueryin classAbstractQueryReport- Parameters:
query- the query that was executed and failedargs- the arguments to the executionname- the name of the method used to executeAbstractCreateStatementInterceptor.isExecute(Method, boolean)start- the wall clock time the query execution starteddelta- the time the execution took, measured with the monotonic clock- Returns:
- - the SQL that was executed or the string "batch" if it was a batch execution, or
nullif the invocation carries no SQL string
-
reportSlowQuery
Description copied from class:AbstractQueryReportInvoked when a query execution, a call to an execute method as recognised byAbstractCreateStatementInterceptor.isExecute(Method, boolean), succeeded and was exceeded the timing threshold- Overrides:
reportSlowQueryin classAbstractQueryReport- Parameters:
query- the query that was executed and failedargs- the arguments to the executionname- the name of the method used to executeAbstractCreateStatementInterceptor.isExecute(Method, boolean)start- the wall clock time the query execution starteddelta- the time the execution took, measured with the monotonic clock- Returns:
- - the SQL that was executed or the string "batch" if it was a batch execution, or
nullif the invocation carries no SQL string
-
closeInvoked
public void closeInvoked()invoked when the connection receives the close request Not used for now.- Specified by:
closeInvokedin classAbstractCreateStatementInterceptor
-
prepareStatement
Tracks the preparation time for a prepared statement.- Specified by:
prepareStatementin classAbstractQueryReport- Parameters:
sql- the SQL statementtime- the time taken to prepare the statement
-
prepareCall
Tracks the preparation time for a callable statement.- Specified by:
prepareCallin classAbstractQueryReport- Parameters:
sql- the SQL calltime- the time taken to prepare the call
-
poolStarted
Initializes the query statistics map for the given connection pool.The map is registered under the pool name together with the pool instance itself. If a live pool is already registered under the name, both pools share one statistics map; that merging is the pre-existing behaviour, but it is a configuration mistake worth a warning, so it is logged.
- Overrides:
poolStartedin classJdbcInterceptor- Parameters:
pool- the connection pool that has started
-
poolClosed
Releases the query statistics map for the given connection pool, unless another live pool is still registered under its name.The callback is delivered by
ConnectionPoolto a freshly instantiated interceptor (seeConnectionPool#close(boolean)), which has never held a reference to the statistics, so the map to release can only be identified through the closing pool passed here, not through this instance's own state. The statistics stay published as long as one pool registered under the name is alive.- Overrides:
poolClosedin classJdbcInterceptor- Parameters:
pool- the connection pool that has been closed
-
releaseStatistics
Deregisters a closed pool from the live pools that the per-name statistics are held for, and releases the statistics once no live pool is registered under the name any more.The decision is taken inside a
computeon the name, which atomises it against the concurrent removals of other same-named pools: exactly one of any number of concurrentpoolCloseddeliveries seestruereturned, so the per-name entries are never released twice and never left behind.- Parameters:
pool- the connection pool that has been closed- Returns:
trueif this call removed the last live pool registered under the name
-
getQueryStats
Retrieves or creates a QueryStats entry for the given SQL string.- Parameters:
sql- the SQL query string- Returns:
- the query statistics, or
nullif the pool has been closed
-
removeOldest
Evicts the least recently invoked query statistics until the map is back withinmaxQueriesentries. The map is shared by all the connections of the pool, so the eviction runs concurrently with the inserts and removals of the other borrower threads and can only do as much work as its own snapshot allows; it then leaves the map above capacity for the next new query to evict further.- Parameters:
queries- The queries map
-
reset
Resets the query statistics reference when a connection is reset.- Overrides:
resetin classAbstractCreateStatementInterceptor- Parameters:
parent- the connection poolcon- the pooled connection being reset
-
isLogSlow
public boolean isLogSlow()Returns whether slow query logging is enabled.- Returns:
trueif slow query logging is enabled
-
setLogSlow
public void setLogSlow(boolean logSlow) Enables or disables slow query logging.- Parameters:
logSlow-trueto enable slow query logging
-
isLogFailed
public boolean isLogFailed()Returns whether failed query logging is enabled.- Returns:
trueif failed query logging is enabled
-
setLogFailed
public void setLogFailed(boolean logFailed) Enables or disables failed query logging.- Parameters:
logFailed-trueto enable failed query logging
-
setProperties
Configures this interceptor from the given properties map, supporting threshold, maxQueries, logSlow, and logFailed settings.- Overrides:
setPropertiesin classJdbcInterceptor- Parameters:
properties- the interceptor properties
-