public class SlowQueryListener extends Object implements QueryExecutionListener
This listener detects slow queries during execution (sometimes called preemptive).
When a query exceeds the specified threshold, the onSlowQuery(ExecutionInfo, List, long) callback is
invoked while the query is still running. The callback is invoked only once for each query that exceeds the
threshold. The running query is not interrupted or cancelled.
NOTE:
ExecutionInfo.getElapsedTime() contains the elapsed time at the moment of the check, which is usually
close to the specified threshold rather than the query's actual execution time.
ExecutionInfo are not available because the query
has not finished.
If you want to log or take action after execution (sometimes called non-preemptive) for queries that exceed a specified threshold, use a regular logging listener like this:
long thresholdInMillis = ...
SLF4JQueryLoggingListener listener = new SLF4JQueryLoggingListener(){
@Override
public void afterQuery(ExecutionInfo execInfo, List<QueryInfo> queryInfoList) {
if (execInfo.getElapsedTime() >= thresholdInMillis) {
super.afterQuery(execInfo, queryInfoList);
}
}
};
This approach makes the actual query execution time available, but does not detect queries that never return.
Both approaches can be used together.CommonsSlowQueryListener,
JULSlowQueryListener,
SLF4JSlowQueryListener,
SystemOutSlowQueryListener| Modifier and Type | Class and Description |
|---|---|
protected static class |
SlowQueryListener.RunningQueryContext
Data holder for the currently running query.
|
| Modifier and Type | Field and Description |
|---|---|
protected ScheduledExecutorService |
executor |
protected Map<String,SlowQueryListener.RunningQueryContext> |
inExecution |
protected StopwatchFactory |
stopwatchFactory |
protected long |
threshold |
protected TimeUnit |
thresholdTimeUnit |
protected boolean |
useDaemonThread |
DEFAULT| Constructor and Description |
|---|
SlowQueryListener() |
| Modifier and Type | Method and Description |
|---|---|
void |
afterQuery(ExecutionInfo execInfo,
List<QueryInfo> queryInfoList) |
void |
beforeQuery(ExecutionInfo execInfo,
List<QueryInfo> queryInfoList) |
protected String |
getExecutionInfoKey(ExecutionInfo executionInfo)
Calculates a key for the given
ExecutionInfo. |
ScheduledExecutorService |
getExecutor() |
long |
getThreshold() |
TimeUnit |
getThresholdTimeUnit() |
protected void |
onSlowQuery(ExecutionInfo execInfo,
List<QueryInfo> queryInfoList,
long startTimeInMills)
Callback invoked when query execution exceeds the threshold.
|
void |
setStopwatchFactory(StopwatchFactory stopwatchFactory)
Sets the
StopwatchFactory used to compute ExecutionInfo.getElapsedTime() for slow queries. |
void |
setThreshold(long threshHold) |
void |
setThresholdTimeUnit(TimeUnit thresholdTimeUnit) |
void |
setUseDaemonThread(boolean useDaemonThread)
Sets whether the executor creates daemon threads to check for slow queries.
|
protected boolean useDaemonThread
protected ScheduledExecutorService executor
protected long threshold
protected TimeUnit thresholdTimeUnit
protected Map<String,SlowQueryListener.RunningQueryContext> inExecution
protected StopwatchFactory stopwatchFactory
public void beforeQuery(ExecutionInfo execInfo, List<QueryInfo> queryInfoList)
beforeQuery in interface QueryExecutionListenerpublic void afterQuery(ExecutionInfo execInfo, List<QueryInfo> queryInfoList)
afterQuery in interface QueryExecutionListenerprotected String getExecutionInfoKey(ExecutionInfo executionInfo)
ExecutionInfo.
This key is passed to the slow-query check Runnable and is also used for removal in
afterQuery(ExecutionInfo, List).
The default implementation uses System.identityHashCode(Object). This does not guarantee 100% uniqueness,
but it is sufficient for the short-lived usage in this class.
Subclasses may override this method to provide a different implementation that uniquely represents
ExecutionInfo.
executionInfo - execution infoprotected void onSlowQuery(ExecutionInfo execInfo, List<QueryInfo> queryInfoList, long startTimeInMills)
This callback is invoked only once per query if it exceeds the threshold.
It is invoked by a thread from the scheduled executor while the query is still running.
ExecutionInfo.getElapsedTime() contains the elapsed time at the moment of the check.
execInfo - query execution infoqueryInfoList - query parameter infostartTimeInMills - time in milliseconds when the query startedpublic void setThreshold(long threshHold)
public void setThresholdTimeUnit(TimeUnit thresholdTimeUnit)
public ScheduledExecutorService getExecutor()
public long getThreshold()
public TimeUnit getThresholdTimeUnit()
public void setUseDaemonThread(boolean useDaemonThread)
useDaemonThread - whether to use daemon threads; defaults to truepublic void setStopwatchFactory(StopwatchFactory stopwatchFactory)
StopwatchFactory used to compute ExecutionInfo.getElapsedTime() for slow queries.stopwatchFactory - factory used to create a Stopwatch for slow-query timingCopyright © 2026. All rights reserved.