Skip to content

Commit

Permalink
Fix for Bug#112884 (Bug#36043166), Setting a large timeout leads to e…
Browse files Browse the repository at this point in the history
…rrors when executing SQL.

Change-Id: I6d0be9e57582c4d66d319445db1776950155e9cd
  • Loading branch information
Axyoan Marcelo committed Dec 4, 2023
1 parent 61422b9 commit 2685f05
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 18 deletions.
2 changes: 2 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

Version 8.3.0

- Fix for Bug#112884 (Bug#36043166), Setting a large timeout leads to errors when executing SQL.

- WL#16077, Upgrade 3rd party libraries and tools.

- WL#16074, Upgrade Protocol Buffers dependency to protobuf-java-3.25.1.
Expand Down
6 changes: 3 additions & 3 deletions src/main/core-api/java/com/mysql/cj/Query.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ public enum CancelStatus {

void setResultType(Resultset.Type resultSetType);

int getTimeoutInMillis();
long getTimeoutInMillis();

void setTimeoutInMillis(int timeoutInMillis);
void setTimeoutInMillis(long timeoutInMillis);

void setExecuteTime(long executeTime);

Expand All @@ -100,7 +100,7 @@ public enum CancelStatus {
*/
long getExecuteTime();

CancelQueryTask startQueryTimer(Query stmtToCancel, int timeout);
CancelQueryTask startQueryTimer(Query stmtToCancel, long timeout);

AtomicBoolean getStatementExecuting();

Expand Down
8 changes: 4 additions & 4 deletions src/main/core-impl/java/com/mysql/cj/AbstractQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public abstract class AbstractQuery implements Query {
private CancelStatus cancelStatus = CancelStatus.NOT_CANCELED;

/** The timeout for a query */
protected int timeoutInMillis = 0;
protected long timeoutInMillis = 0L;

/** Holds batched commands */
protected List<Object> batchedArgs;
Expand Down Expand Up @@ -206,17 +206,17 @@ public void setResultType(Resultset.Type resultSetType) {
}

@Override
public int getTimeoutInMillis() {
public long getTimeoutInMillis() {
return this.timeoutInMillis;
}

@Override
public void setTimeoutInMillis(int timeoutInMillis) {
public void setTimeoutInMillis(long timeoutInMillis) {
this.timeoutInMillis = timeoutInMillis;
}

@Override
public CancelQueryTask startQueryTimer(Query stmtToCancel, int timeout) {
public CancelQueryTask startQueryTimer(Query stmtToCancel, long timeout) {
if (this.session.getPropertySet().getBooleanProperty(PropertyKey.enableQueryTimeouts).getValue() && timeout != 0) {
CancelQueryTaskImpl timeoutTask = new CancelQueryTaskImpl(stmtToCancel);
this.session.getCancelTimer().schedule(timeoutTask, timeout);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ protected long[] executeBatchInternal() throws SQLException {
}

// we timeout the entire batch, not individual statements
int batchTimeout = getTimeoutInMillis();
long batchTimeout = getTimeoutInMillis();
setTimeoutInMillis(0);

resetCancelledState();
Expand Down Expand Up @@ -432,7 +432,7 @@ protected long[] executeBatchInternal() throws SQLException {
* @throws SQLException
* if a database access error occurs or this method is called on a closed PreparedStatement
*/
protected long[] executePreparedBatchAsMultiStatement(int batchTimeout) throws SQLException {
protected long[] executePreparedBatchAsMultiStatement(long batchTimeout) throws SQLException {
synchronized (checkClosed().getConnectionMutex()) {
// This is kind of an abuse, but it gets the job done
if (this.batchedValuesClause == null) {
Expand Down Expand Up @@ -618,7 +618,7 @@ private String generateMultiStatementForBatch(int numBatches) throws SQLExceptio
* @throws SQLException
* if a database access error occurs or this method is called on a closed PreparedStatement
*/
protected long[] executeBatchWithMultiValuesClause(int batchTimeout) throws SQLException {
protected long[] executeBatchWithMultiValuesClause(long batchTimeout) throws SQLException {
synchronized (checkClosed().getConnectionMutex()) {
JdbcConnection locallyScopedConn = this.connection;

Expand Down Expand Up @@ -746,7 +746,7 @@ protected long[] executeBatchWithMultiValuesClause(int batchTimeout) throws SQLE
* @throws SQLException
* if an error occurs
*/
protected long[] executeBatchSerially(int batchTimeout) throws SQLException {
protected long[] executeBatchSerially(long batchTimeout) throws SQLException {
synchronized (checkClosed().getConnectionMutex()) {
if (this.connection == null) {
checkClosed();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ public void close() throws SQLException {
}

@Override
protected long[] executeBatchSerially(int batchTimeout) throws SQLException {
protected long[] executeBatchSerially(long batchTimeout) throws SQLException {
synchronized (checkClosed().getConnectionMutex()) {
JdbcConnection locallyScopedConn = this.connection;

Expand Down
12 changes: 6 additions & 6 deletions src/main/user-impl/java/com/mysql/cj/jdbc/StatementImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ protected void setupStreamingTimeout(JdbcConnection con) throws SQLException {
}

@Override
public CancelQueryTask startQueryTimer(Query stmtToCancel, int timeout) {
public CancelQueryTask startQueryTimer(Query stmtToCancel, long timeout) {
return this.query.startQueryTimer(stmtToCancel, timeout);
}

Expand Down Expand Up @@ -820,7 +820,7 @@ protected long[] executeBatchInternal() throws SQLException {
}

// we timeout the entire batch, not individual statements
int individualStatementTimeout = getTimeoutInMillis();
long individualStatementTimeout = getTimeoutInMillis();
setTimeoutInMillis(0);

CancelQueryTask timeoutTask = null;
Expand Down Expand Up @@ -952,7 +952,7 @@ protected final boolean hasDeadlockOrTimeoutRolledBackTx(SQLException ex) {
* @throws SQLException
* if a database access error occurs or this method is called on a closed PreparedStatement
*/
private long[] executeBatchUsingMultiQueries(boolean multiQueriesEnabled, int nbrCommands, int individualStatementTimeout) throws SQLException {
private long[] executeBatchUsingMultiQueries(boolean multiQueriesEnabled, int nbrCommands, long individualStatementTimeout) throws SQLException {
JdbcConnection locallyScopedConn = checkClosed();

synchronized (locallyScopedConn.getConnectionMutex()) {
Expand Down Expand Up @@ -1614,7 +1614,7 @@ public boolean getMoreResults(int current) throws SQLException {
@Override
public int getQueryTimeout() throws SQLException {
synchronized (checkClosed().getConnectionMutex()) {
return getTimeoutInMillis() / 1000;
return Math.toIntExact(getTimeoutInMillis() / 1000);
}
}

Expand Down Expand Up @@ -2206,12 +2206,12 @@ public void setResultType(Resultset.Type resultSetType) {
}

@Override
public int getTimeoutInMillis() {
public long getTimeoutInMillis() {
return this.query.getTimeoutInMillis();
}

@Override
public void setTimeoutInMillis(int timeoutInMillis) {
public void setTimeoutInMillis(long timeoutInMillis) {
this.query.setTimeoutInMillis(timeoutInMillis);
}

Expand Down
12 changes: 12 additions & 0 deletions src/test/java/testsuite/regression/StatementRegressionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13543,4 +13543,16 @@ void testBug109546() throws Exception {
});
}

/**
* Tests fix for Bug#112884 (Bug#36043166), Setting a large timeout leads to errors when executing SQL.
*
* @throws Exception
*/
@Test
void testBug112884() throws Exception {
assertDoesNotThrow(() -> {
this.stmt.setQueryTimeout(Integer.MAX_VALUE / 1000 + 1);
});
}

}

0 comments on commit 2685f05

Please sign in to comment.