# Class SlowQueryReport

java.lang.Object
org.apache.tomcat.jdbc.pool.JdbcInterceptor
org.apache.tomcat.jdbc.pool.interceptor.AbstractCreateStatementInterceptor
org.apache.tomcat.jdbc.pool.interceptor.AbstractQueryReport
org.apache.tomcat.jdbc.pool.interceptor.SlowQueryReport

All Implemented Interfaces:
  `InvocationHandler`

Direct Known Subclasses:
  `SlowQueryReportJmx`

public class SlowQueryReport extends AbstractQueryReport
Slow query report interceptor. Tracks timing of query executions.

## Nested Class Summary

Nested Classes
Modifier and Type
Class
Description
`static class`
`SlowQueryReport.QueryStats`
Holds timing and invocation statistics for a single SQL query.
`static class`
`SlowQueryReport.QueryStatsComparator`
Compares QueryStats by their lastInvocation value.

### Nested classes/interfaces inherited from class AbstractQueryReport

`AbstractQueryReport.StatementProxy`

## Field Summary

Fields
Modifier and Type
Field
Description
`protected boolean`
`logFailed`
Flag to enable disable logging of failed queries
`protected boolean`
`logSlow`
Flag to enable disable logging of slow queries
`protected int`
`maxQueries`
Maximum number of queries we will be storing
`protected static final ConcurrentHashMap<String, ConcurrentHashMap<String, SlowQueryReport.QueryStats>>`
`perPoolStats`
we will be keeping track of query stats on a per pool basis
`protected ConcurrentHashMap<String, SlowQueryReport.QueryStats>`
`queries`
the queries that are used for this interceptor.
`protected final Comparator<SlowQueryReport.QueryStats>`
`queryStatsComparator`
Sort QueryStats by last invocation time

### Fields inherited from class AbstractQueryReport

`threshold`

### Fields inherited from class AbstractCreateStatementInterceptor

`constructors, CREATE_STATEMENT, CREATE_STATEMENT_IDX, EXECUTE, EXECUTE_BATCH, 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_TYPES`

### Fields inherited from class 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
`SlowQueryReport()`
Creates a slow query report interceptor

## Method Summary

Modifier and Type
Method
Description
`void`
`closeInvoked()`
invoked when the connection receives the close request Not used for now.
`static ConcurrentHashMap<String, SlowQueryReport.QueryStats>`
`getPoolStats(String poolname)`
Returns the query stats for a given pool
`protected SlowQueryReport.QueryStats`
`getQueryStats(String sql)`
Retrieves or creates a QueryStats entry for the given SQL string.
`boolean`
`isLogFailed()`
Returns whether failed query logging is enabled.
`boolean`
`isLogSlow()`
Returns whether slow query logging is enabled.
`void`
`poolClosed(ConnectionPool pool)`
Removes the query statistics map for the given connection pool.
`void`
`poolStarted(ConnectionPool pool)`
Initializes the query statistics map for the given connection pool.
`void`
`prepareCall(String sql, long time)`
Tracks the preparation time for a callable statement.
`void`
`prepareStatement(String sql, long time)`
Tracks the preparation time for a prepared statement.
`protected void`
`removeOldest(ConcurrentHashMap<String, SlowQueryReport.QueryStats> queries)`
Sort QueryStats by last invocation time
`protected String`
`reportFailedQuery(String query, Object[] args, String name, long start, Throwable t)`
Invoked when a query execution, a call to execute/executeQuery or executeBatch failed.
`protected String`
`reportQuery(String query, Object[] args, String name, long start, long delta)`
Invoked when a query execution, a call to execute/executeQuery or executeBatch succeeded and was within the timing threshold
`protected String`
`reportSlowQuery(String query, Object[] args, String name, long start, long delta)`
Invoked when a query execution, a call to execute/executeQuery or executeBatch succeeded and was exceeded the timing threshold
`void`
`reset(ConnectionPool parent, PooledConnection con)`
Resets the query statistics reference when a connection is reset.
`void`
`setLogFailed(boolean logFailed)`
Enables or disables failed query logging.
`void`
`setLogSlow(boolean logSlow)`
Enables or disables slow query logging.
`void`
`setMaxQueries(int maxQueries)`
Sets the maximum number of queries to track statistics for.
`void`
`setProperties(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, setThreshold`

### Methods inherited from class AbstractCreateStatementInterceptor

`getConstructor, invoke, isExecute, isStatement, process`

### Methods inherited from class JdbcInterceptor

`compare, compare, disconnected, getNext, getProperties, isUseEquals, setNext, setUseEquals`

### Methods inherited from class Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Field Details

### perPoolStats

protected static final ConcurrentHashMap<String, ConcurrentHashMap<String, SlowQueryReport.QueryStats>> perPoolStats
we will be keeping track of query stats on a per pool basis

### queries

protected volatile ConcurrentHashMap<String, SlowQueryReport.QueryStats> queries
the queries that are used for this interceptor.

### maxQueries

protected int maxQueries
Maximum number of queries we will be storing

### logSlow

protected boolean logSlow
Flag to enable disable logging of slow queries

### logFailed

protected boolean logFailed
Flag to enable disable logging of failed queries

### queryStatsComparator

protected final Comparator<SlowQueryReport.QueryStats> queryStatsComparator
Sort QueryStats by last invocation time

## Constructor Details

### SlowQueryReport

public SlowQueryReport()
Creates a slow query report interceptor

## Method Details

### getPoolStats

public static ConcurrentHashMap<String, SlowQueryReport.QueryStats> getPoolStats(String poolname)
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 statistics for 0 to maxQueries

### 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, Throwable t)
Description copied from class: `AbstractQueryReport`
Invoked when a query execution, a call to execute/executeQuery or executeBatch failed.

Overrides:
  `reportFailedQuery` in class `AbstractQueryReport`
Parameters:
  `query` - the query that was executed and failed
  `args` - the arguments to the execution
  `name` - the name of the method used to execute `AbstractCreateStatementInterceptor.isExecute(Method, boolean)`
  `start` - the time the query execution started
  `t` - the exception that happened
Returns:
  - the SQL that was executed or the string "batch" if it was a batch execution

### reportQuery

protected String reportQuery(String query, Object[] args, String name, long start, long delta)
Description copied from class: `AbstractQueryReport`
Invoked when a query execution, a call to execute/executeQuery or executeBatch succeeded and was within the timing threshold

Overrides:
  `reportQuery` in class `AbstractQueryReport`
Parameters:
  `query` - the query that was executed and failed
  `args` - the arguments to the execution
  `name` - the name of the method used to execute `AbstractCreateStatementInterceptor.isExecute(Method, boolean)`
  `start` - the time the query execution started
  `delta` - the time the execution took
Returns:
  - the SQL that was executed or the string "batch" if it was a batch execution

### reportSlowQuery

protected String reportSlowQuery(String query, Object[] args, String name, long start, long delta)
Description copied from class: `AbstractQueryReport`
Invoked when a query execution, a call to execute/executeQuery or executeBatch succeeded and was exceeded the timing threshold

Overrides:
  `reportSlowQuery` in class `AbstractQueryReport`
Parameters:
  `query` - the query that was executed and failed
  `args` - the arguments to the execution
  `name` - the name of the method used to execute `AbstractCreateStatementInterceptor.isExecute(Method, boolean)`
  `start` - the time the query execution started
  `delta` - the time the execution took
Returns:
  - the SQL that was executed or the string "batch" if it was a batch execution

### closeInvoked

public void closeInvoked()
invoked when the connection receives the close request Not used for now.

Specified by:
  `closeInvoked` in class `AbstractCreateStatementInterceptor`

### prepareStatement

public void prepareStatement(String sql, long time)
Tracks the preparation time for a prepared statement.

Specified by:
  `prepareStatement` in class `AbstractQueryReport`
Parameters:
  `sql` - the SQL statement
  `time` - the time taken to prepare the statement

### prepareCall

public void prepareCall(String sql, long time)
Tracks the preparation time for a callable statement.

Specified by:
  `prepareCall` in class `AbstractQueryReport`
Parameters:
  `sql` - the SQL call
  `time` - the time taken to prepare the call

### poolStarted

public void poolStarted(ConnectionPool pool)
Initializes the query statistics map for the given connection pool.

Overrides:
  `poolStarted` in class `JdbcInterceptor`
Parameters:
  `pool` - the connection pool that has started

### poolClosed

public void poolClosed(ConnectionPool pool)
Removes the query statistics map for the given connection pool.

Overrides:
  `poolClosed` in class `JdbcInterceptor`
Parameters:
  `pool` - the connection pool that has been closed

### getQueryStats

protected SlowQueryReport.QueryStats getQueryStats(String sql)
Retrieves or creates a QueryStats entry for the given SQL string.

Parameters:
  `sql` - the SQL query string
Returns:
  the query statistics, or `null` if the pool has been closed

### removeOldest

protected void removeOldest(ConcurrentHashMap<String, SlowQueryReport.QueryStats> queries)
Sort QueryStats by last invocation time

Parameters:
  `queries` - The queries map

### reset

public void reset(ConnectionPool parent, PooledConnection con)
Resets the query statistics reference when a connection is reset.

Overrides:
  `reset` in class `AbstractCreateStatementInterceptor`
Parameters:
  `parent` - the connection pool
  `con` - the pooled connection being reset

### isLogSlow

public boolean isLogSlow()
Returns whether slow query logging is enabled.

Returns:
  `true` if slow query logging is enabled

### setLogSlow

public void setLogSlow(boolean logSlow)
Enables or disables slow query logging.

Parameters:
  `logSlow` - `true` to enable slow query logging

### isLogFailed

public boolean isLogFailed()
Returns whether failed query logging is enabled.

Returns:
  `true` if failed query logging is enabled

### setLogFailed

public void setLogFailed(boolean logFailed)
Enables or disables failed query logging.

Parameters:
  `logFailed` - `true` to enable failed query logging

### setProperties

public void setProperties(Map<String, PoolProperties.InterceptorProperty> properties)
Configures this interceptor from the given properties map, supporting threshold, maxQueries, logSlow, and logFailed settings.

Overrides:
  `setProperties` in class `JdbcInterceptor`
Parameters:
  `properties` - the interceptor properties