Slow SQL Logger

Many performance problems in Enterprise Applications can be traced to slow database access. Payara Server Community includes capabilities to detect and log slow SQL queries executed via a JDBC Connection pool. The Slow SQL logger monitors all queries executed on the connection pool and if t hey exceed a configurable execution time in seconds a warning message is logged into the server log.

The warning message (see below) logs the SQL query and the stack trace to the code executing the query. This enables rapid diagnosis by pinpointing the exact lines of code to investigate.

Configuring the Slow SQL Logger

There are a number of ways to configure the slow SQL threshold time on a JDBC connection pool.

Using the Administration Console

To configure slow SQL logging on a JDBC connection pool via the administration console, navigate to the Connection Pool’s Advanced Properties tab. Then specify the Slow Query Log Threshold time in seconds or use a decimal value to signify milliseconds. The value -1 disables the feature:

Slow SQL Logging settings in the administration console

Using Asadmin commands

The Slow Query Log Threshold time for a JDBC connection pool can also be configured with an asadmin command, by using the set command to set the slow-query-threshold-in-seconds property, like in the following example:

asadmin set domain.resources.jdbc-connection-pool.__TimerPool.slow-query-threshold-in-seconds=50

You can also set the threshold time to milliseconds simply by passing a decimal value. Below is a example of setting the threshold on the __TimerPool JDBC connection pool to 200 milliseconds:

asadmin set domain.resources.jdbc-connection-pool.__TimerPool.slow-query-threshold-in-seconds=0.2

Application Deployment

In Java EE 7 applications, a JDBC data source can be deployed by adding the @DataSourceDefinition annotation to a managed component (like and @Stateless EJB bean for example). The Slow Query Log Threshold time can be configured using this annotation, like in the following example:

    name = "java:app/MyApp/MyDS",
    className = "org.h2.jdbcx.JdbcDataSource",
    url = "jdbc:h2:mem:test",
    properties = {"fish.payara.slow-query-threshold-in-seconds=5"})

The Data source definition can also be added to a deployment descriptor of an application, for example in the web.xml standard deployment descriptor:

    <!-- Example of how to use a Payara specific custom connection pool setting -->

Example Trace

Below is an example WARNING trace for a slow query recollected from the server’s log:

[#|2016-02-01T22:39:29.289+0000|WARNING|Payara 4.1||_ThreadID=61;_ThreadName=http-listener-1(2);_TimeMillis=1454366369289;_LevelValue=900;|
  SQL Query Exceeded Threshold Time: 5000(ms): Time Taken: 10000(ms)

java.lang.Exception: Stack Trace shows code path to SQL
    at fish.payara.jdbc.SlowSQLLogger.sqlTrace(
    at com.sun.gjc.util.SQLTraceDelegator.sqlTrace(
    at com.sun.gjc.spi.jdbc40.ProfiledConnectionWrapper40$1.invoke(
    at com.sun.proxy.$Proxy265.executeQuery(Unknown Source)
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeSelect(
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.basicExecuteCall(
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeCall(
    at org.eclipse.persistence.internal.sessions.AbstractSession.basicExecuteCall(
    at org.eclipse.persistence.sessions.server.ServerSession.executeCall(
    at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeCall(
    at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeCall(
    at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeSelectCall(
    at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.selectAllRows(
    at org.eclipse.persistence.internal.queries.ExpressionQueryMechanism.selectAllRowsFromTable(
    at org.eclipse.persistence.internal.queries.ExpressionQueryMechanism.selectAllRows(
    at org.eclipse.persistence.queries.ReadAllQuery.executeObjectLevelReadQuery(
    at org.eclipse.persistence.queries.ObjectLevelReadQuery.executeDatabaseQuery(
    at org.eclipse.persistence.queries.DatabaseQuery.execute(
    at org.eclipse.persistence.queries.ObjectLevelReadQuery.execute(
    at org.eclipse.persistence.queries.ReadAllQuery.execute(
    at org.eclipse.persistence.queries.ObjectLevelReadQuery.executeInUnitOfWork(
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.internalExecuteQuery(
    at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(
    at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(
    at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(
    at org.eclipse.persistence.internal.jpa.QueryImpl.executeReadQuery(
    at org.eclipse.persistence.internal.jpa.QueryImpl.getResultList(

Payara Micro Support

Payara Micro Community also supports slow logging of SQL queries which brings powerful operational diagnostics to your micro-services platform. To enable it on applications deployed on Payara Micro, use the annotation or deployment descriptor methods described in the previous section.