Skip to content

Commit 9ebb7a7

Browse files
added statement timeout
1 parent fa0d21b commit 9ebb7a7

File tree

14 files changed

+54
-29
lines changed

14 files changed

+54
-29
lines changed

jdbc-perf-logger-driver/src/main/java/ch/sla/jdbcperflogger/driver/LoggingPreparedStatementInvocationHandler.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public Object invoke(@Nullable final Object proxy, @Nullable final Method _metho
9292
protected ResultSet internalExecutePreparedQuery(final Method method) throws Throwable {
9393
final UUID logId = UUID.randomUUID();
9494
PerfLogger.logBeforePreparedStatement(connectionId, logId, rawSql, paramValues,
95-
StatementType.PREPARED_QUERY_STMT, databaseType);
95+
StatementType.PREPARED_QUERY_STMT, databaseType, wrappedStatement.getQueryTimeout());
9696
final long start = System.nanoTime();
9797
Throwable exc = null;
9898
try {
@@ -116,7 +116,7 @@ protected Object internalExecutePrepared(final Method method, @Nullable final Ob
116116
final UUID logId = UUID.randomUUID();
117117
final long start = System.nanoTime();
118118
PerfLogger.logBeforePreparedStatement(connectionId, logId, rawSql, paramValues,
119-
StatementType.BASE_PREPARED_STMT, databaseType);
119+
StatementType.BASE_PREPARED_STMT, databaseType, wrappedStatement.getQueryTimeout());
120120
Throwable exc = null;
121121
try {
122122
return Utils.invokeUnwrapException(wrappedStatement, method, args);
@@ -134,7 +134,7 @@ protected Object internalExecutePrepared(final Method method, @Nullable final Ob
134134
protected Object internalExecuteBatch(final Method method, @Nullable final Object[] args) throws Throwable {
135135
final UUID logId = UUID.randomUUID();
136136
PerfLogger.logPreparedBatchedStatements(connectionId, rawSql, batchedPreparedOrNonPreparedStmtExecutions,
137-
databaseType);
137+
databaseType, wrappedStatement.getQueryTimeout());
138138
final long start = System.nanoTime();
139139
Throwable exc = null;
140140
try {

jdbc-perf-logger-driver/src/main/java/ch/sla/jdbcperflogger/driver/LoggingStatementInvocationHandler.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ public Object invoke(final @Nullable Object _proxy, @Nullable final Method _meth
8181
protected final ResultSet internalExecuteQuery(final Method method, final Object[] args) throws Throwable {
8282
final UUID logId = UUID.randomUUID();
8383
final long start = System.nanoTime();
84-
PerfLogger.logBeforeStatement(connectionId, logId, (String) args[0], StatementType.NON_PREPARED_QUERY_STMT);
84+
PerfLogger.logBeforeStatement(connectionId, logId, (String) args[0], StatementType.NON_PREPARED_QUERY_STMT,
85+
wrappedStatement.getQueryTimeout());
8586
Throwable exc = null;
8687
try {
8788
final ResultSet resultSet = (ResultSet) Utils.invokeUnwrapExceptionReturnNonNull(wrappedStatement, method,
@@ -102,7 +103,8 @@ protected final ResultSet internalExecuteQuery(final Method method, final Object
102103
@Nullable
103104
protected final Object internalExecute(final Method method, final Object[] args) throws Throwable {
104105
final UUID logId = UUID.randomUUID();
105-
PerfLogger.logBeforeStatement(connectionId, logId, (String) args[0], StatementType.BASE_NON_PREPARED_STMT);
106+
PerfLogger.logBeforeStatement(connectionId, logId, (String) args[0], StatementType.BASE_NON_PREPARED_STMT,
107+
wrappedStatement.getQueryTimeout());
106108
Throwable exc = null;
107109
final long start = System.nanoTime();
108110
try {
@@ -119,7 +121,8 @@ protected final Object internalExecute(final Method method, final Object[] args)
119121
@Nullable
120122
protected Object internalExecuteBatch(final Method method, @Nullable final Object[] args) throws Throwable {
121123
final UUID logId = UUID.randomUUID();
122-
PerfLogger.logNonPreparedBatchedStatements(connectionId, logId, batchedNonPreparedStmtExecutions, databaseType);
124+
PerfLogger.logNonPreparedBatchedStatements(connectionId, logId, batchedNonPreparedStmtExecutions, databaseType,
125+
wrappedStatement.getQueryTimeout());
123126
Throwable exc = null;
124127
final long start = System.nanoTime();
125128
try {

jdbc-perf-logger-driver/src/main/java/ch/sla/jdbcperflogger/logger/PerfLogger.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,18 +73,18 @@ public class PerfLogger {
7373
}
7474

7575
public static void logBeforeStatement(final UUID connectionId, final UUID logId, final String sql,
76-
final StatementType statementType) {
76+
final StatementType statementType, final int timeout) {
7777
if (LOGGER_ORIGINAL_SQL.isDebugEnabled()) {
7878
LOGGER_ORIGINAL_SQL.debug("Before execution of non-prepared stmt {}: {}", logId, sql);
7979
}
8080
final long now = System.currentTimeMillis();
8181
PerfLoggerRemoting.postLog(new StatementLog(connectionId, logId, now, statementType, sql, Thread
82-
.currentThread().getName()));
82+
.currentThread().getName(), timeout));
8383
}
8484

8585
public static void logBeforePreparedStatement(final UUID connectionId, final UUID logId, final String rawSql,
8686
final PreparedStatementValuesHolder pstmtValues, final StatementType statementType,
87-
final DatabaseType databaseType) {
87+
final DatabaseType databaseType, final int timeout) {
8888
if (LOGGER_ORIGINAL_SQL.isDebugEnabled()) {
8989
LOGGER_ORIGINAL_SQL.debug("Before execution of prepared stmt {}: {}", logId, rawSql);
9090
}
@@ -94,11 +94,11 @@ public static void logBeforePreparedStatement(final UUID connectionId, final UUI
9494
}
9595
final long now = System.currentTimeMillis();
9696
PerfLoggerRemoting.postLog(new StatementLog(connectionId, logId, now, statementType, rawSql, filledSql, Thread
97-
.currentThread().getName()));
97+
.currentThread().getName(), timeout));
9898
}
9999

100100
public static void logNonPreparedBatchedStatements(final UUID connectionId, final UUID logId,
101-
final List<String> batchedExecutions, final DatabaseType databaseType) {
101+
final List<String> batchedExecutions, final DatabaseType databaseType, final int timeout) {
102102

103103
final long now = System.currentTimeMillis();
104104
if (LOGGER_ORIGINAL_SQL.isDebugEnabled()) {
@@ -112,11 +112,11 @@ public static void logNonPreparedBatchedStatements(final UUID connectionId, fina
112112
}
113113
}
114114
PerfLoggerRemoting.postLog(new BatchedNonPreparedStatementsLog(connectionId, logId, now, batchedExecutions,
115-
Thread.currentThread().getName()));
115+
Thread.currentThread().getName(), timeout));
116116
}
117117

118118
public static void logPreparedBatchedStatements(final UUID connectionId, final String rawSql,
119-
final List<Object> batchedExecutions, final DatabaseType databaseType) {
119+
final List<Object> batchedExecutions, final DatabaseType databaseType, final int timeout) {
120120
final long now = System.currentTimeMillis();
121121
if (LOGGER_ORIGINAL_SQL.isDebugEnabled()) {
122122
LOGGER_ORIGINAL_SQL.debug("Before execution of {} batched prepared statements with raw sql {}",
@@ -138,7 +138,7 @@ public static void logPreparedBatchedStatements(final UUID connectionId, final S
138138
}
139139
}
140140
PerfLoggerRemoting.postLog(new BatchedPreparedStatementsLog(connectionId, UUID.randomUUID(), now, rawSql,
141-
filledSqlList, Thread.currentThread().getName()));
141+
filledSqlList, Thread.currentThread().getName(), timeout));
142142
}
143143

144144
public static void logStatementExecuted(final UUID logId, final long durationNanos,

jdbc-perf-logger-driver/src/main/java/ch/sla/jdbcperflogger/model/AbstractBeforeStatementExecutionLog.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,16 @@ public class AbstractBeforeStatementExecutionLog implements LogMessage {
2828
private final long timestamp;
2929
private final StatementType statementType;
3030
private final String threadName;
31+
private final int timeout;
3132

3233
public AbstractBeforeStatementExecutionLog(final UUID connectionId, final UUID logId, final long timestamp,
33-
final StatementType statementType, final String threadName) {
34+
final StatementType statementType, final String threadName, final int timeout) {
3435
connectionUuid = connectionId;
3536
this.logId = logId;
3637
this.timestamp = timestamp;
3738
this.statementType = statementType;
3839
this.threadName = threadName;
40+
this.timeout = timeout;
3941
}
4042

4143
public UUID getConnectionUuid() {
@@ -58,4 +60,8 @@ public String getThreadName() {
5860
return threadName;
5961
}
6062

63+
public int getTimeout() {
64+
return timeout;
65+
}
66+
6167
}

jdbc-perf-logger-driver/src/main/java/ch/sla/jdbcperflogger/model/BatchedNonPreparedStatementsLog.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ public class BatchedNonPreparedStatementsLog extends AbstractBeforeStatementExec
2929
private final List<String> sqlList;
3030

3131
public BatchedNonPreparedStatementsLog(final UUID connectionId, final UUID logId, final long timestamp,
32-
final List<String> sqlList, final String threadName) {
33-
super(connectionId, logId, timestamp, StatementType.NON_PREPARED_BATCH_EXECUTION, threadName);
32+
final List<String> sqlList, final String threadName, final int timeout) {
33+
super(connectionId, logId, timestamp, StatementType.NON_PREPARED_BATCH_EXECUTION, threadName, timeout);
3434
this.sqlList = Collections.unmodifiableList(new ArrayList<String>(sqlList));
3535
}
3636

jdbc-perf-logger-driver/src/main/java/ch/sla/jdbcperflogger/model/BatchedPreparedStatementsLog.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ public class BatchedPreparedStatementsLog extends AbstractBeforeStatementExecuti
3030
private final List<String> sqlList;
3131

3232
public BatchedPreparedStatementsLog(final UUID connectionId, final UUID logId, final long timestamp,
33-
final String rawSql, final List<String> sqlList, final String threadName) {
34-
super(connectionId, logId, timestamp, StatementType.PREPARED_BATCH_EXECUTION, threadName);
33+
final String rawSql, final List<String> sqlList, final String threadName, final int timeout) {
34+
super(connectionId, logId, timestamp, StatementType.PREPARED_BATCH_EXECUTION, threadName, timeout);
3535
this.rawSql = rawSql;
3636
this.sqlList = Collections.unmodifiableList(new ArrayList<String>(sqlList));
3737
}

jdbc-perf-logger-driver/src/main/java/ch/sla/jdbcperflogger/model/StatementLog.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,17 @@ public class StatementLog extends AbstractBeforeStatementExecutionLog {
2828
private final boolean preparedStatement;
2929

3030
public StatementLog(final UUID connectionId, final UUID logId, final long timestamp,
31-
final StatementType statementType, final String sql, final String threadName) {
32-
super(connectionId, logId, timestamp, statementType, threadName);
31+
final StatementType statementType, final String sql, final String threadName, final int timeout) {
32+
super(connectionId, logId, timestamp, statementType, threadName, timeout);
3333
rawSql = sql;
3434
filledSql = sql;
3535
preparedStatement = false;
3636
}
3737

3838
public StatementLog(final UUID connectionId, final UUID logId, final long timestamp,
39-
final StatementType statementType, final String rawSql, final String filledSql, final String threadName) {
40-
super(connectionId, logId, timestamp, statementType, threadName);
39+
final StatementType statementType, final String rawSql, final String filledSql, final String threadName,
40+
final int timeout) {
41+
super(connectionId, logId, timestamp, statementType, threadName, timeout);
4142
this.rawSql = rawSql;
4243
this.filledSql = filledSql;
4344
preparedStatement = true;

jdbc-perf-logger-driver/src/test/java/ch/sla/jdbcperflogger/driver/WrappingDriverTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,17 @@ public void testSelectNonPrepared() throws Exception {
109109
statement.close();
110110
}
111111

112+
@Test
113+
public void testTimeoutSelectNonPrepared() throws Exception {
114+
final Statement statement = connection.createStatement();
115+
statement.setQueryTimeout(123);
116+
statement.execute("create table test (key_id int);");
117+
final StatementLog statementLog = (StatementLog) lastLogMessage2;
118+
assert statementLog != null;
119+
assertEquals(123, statementLog.getTimeout());
120+
statement.close();
121+
}
122+
112123
@SuppressWarnings("null")
113124
@Test
114125
public void testExecutePrepared() throws Exception {

jdbc-perf-logger-gui/src/main/java/ch/sla/jdbcperflogger/console/db/LogRepositoryConstants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public class LogRepositoryConstants {
1212
public static final String EXEC_PLUS_FETCH_TIME_COLUMN = "EXEC_PLUS_FETCH_TIME";
1313
public static final String THREAD_NAME_COLUMN = "threadName";
1414
public static final String CONNECTION_NUMBER_COLUMN = "connectionNumber";
15+
public static final String TIMEOUT_COLUMN = "TIMEOUT";
1516
public static final String ERROR_COLUMN = "ERROR";
1617
public static final String EXEC_COUNT_COLUMN = "EXEC_COUNT";
1718
public static final String TOTAL_EXEC_TIME_COLUMN = "TOTAL_EXEC_TIME";

jdbc-perf-logger-gui/src/main/java/ch/sla/jdbcperflogger/console/db/LogRepositoryReadJdbc.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public void getStatements(final LogSearchCriteria searchCriteria, final ResultSe
6161
final boolean withFilledSql) {
6262
final StringBuilder sql = new StringBuilder("select id, tstamp, statementType, rawSql, " //
6363
+ "exec_plus_fetch_time, execution_time, fetch_time, "//
64-
+ "nbRowsIterated, threadName, connectionNumber, error ");
64+
+ "nbRowsIterated, threadName, connectionNumber, timeout, error ");
6565
if (withFilledSql) {
6666
sql.append(", " + LogRepositoryConstants.FILLED_SQL_COLUMN);
6767
}

0 commit comments

Comments
 (0)