Skip to content

Commit 383458f

Browse files
authored
Fix JDBC reusing stale statement id after reconnect (#18078)
1 parent 4b1d5e5 commit 383458f

1 file changed

Lines changed: 31 additions & 4 deletions

File tree

iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBStatement.java

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,14 @@ private boolean executeSQL(String sql) throws TException, SQLException {
363363
execReq.setTimeout((long) queryTimeout * 1000);
364364
TSExecuteStatementResp execResp =
365365
callWithRetryAndReconnect(
366-
() -> client.executeStatementV2(execReq), TSExecuteStatementResp::getStatus);
366+
() -> {
367+
// reConnect() may have replaced the session/statement id, so refresh them on every
368+
// attempt; otherwise the server reports "StatementId doesn't exist in this session".
369+
execReq.setSessionId(sessionId);
370+
execReq.setStatementId(stmtId);
371+
return client.executeStatementV2(execReq);
372+
},
373+
TSExecuteStatementResp::getStatus);
367374

368375
if (execResp.isSetOperationType() && execResp.getOperationType().equals("dropDB")) {
369376
connection.changeDefaultDatabase(null);
@@ -430,7 +437,13 @@ private int[] executeBatchSQL() throws TException, BatchUpdateException, SQLExce
430437
isCancelled = false;
431438
TSExecuteBatchStatementReq execReq = new TSExecuteBatchStatementReq(sessionId, batchSQLList);
432439
TSStatus execResp =
433-
callWithRetryAndReconnect(() -> client.executeBatchStatement(execReq), status -> status);
440+
callWithRetryAndReconnect(
441+
() -> {
442+
// reConnect() may have replaced the session id, so refresh it on every attempt.
443+
execReq.setSessionId(sessionId);
444+
return client.executeBatchStatement(execReq);
445+
},
446+
status -> status);
434447
int[] result = new int[batchSQLList.size()];
435448
boolean allSuccess = true;
436449
StringBuilder message = new StringBuilder(System.lineSeparator());
@@ -500,7 +513,14 @@ private ResultSet executeQuerySQL(String sql, long timeoutInMS) throws TExceptio
500513
execReq.setJdbcQuery(true);
501514
TSExecuteStatementResp execResp =
502515
callWithRetryAndReconnect(
503-
() -> client.executeQueryStatementV2(execReq), TSExecuteStatementResp::getStatus);
516+
() -> {
517+
// reConnect() may have replaced the session/statement id, so refresh them on every
518+
// attempt; otherwise the server reports "StatementId doesn't exist in this session".
519+
execReq.setSessionId(sessionId);
520+
execReq.setStatementId(stmtId);
521+
return client.executeQueryStatementV2(execReq);
522+
},
523+
TSExecuteStatementResp::getStatus);
504524
queryId = execResp.getQueryId();
505525
try {
506526
RpcUtils.verifySuccess(execResp.getStatus());
@@ -575,7 +595,14 @@ private int executeUpdateSQL(final String sql)
575595
final TSExecuteStatementReq execReq = new TSExecuteStatementReq(sessionId, sql, stmtId);
576596
final TSExecuteStatementResp execResp =
577597
callWithRetryAndReconnect(
578-
() -> client.executeUpdateStatement(execReq), TSExecuteStatementResp::getStatus);
598+
() -> {
599+
// reConnect() may have replaced the session/statement id, so refresh them on every
600+
// attempt; otherwise the server reports "StatementId doesn't exist in this session".
601+
execReq.setSessionId(sessionId);
602+
execReq.setStatementId(stmtId);
603+
return client.executeUpdateStatement(execReq);
604+
},
605+
TSExecuteStatementResp::getStatus);
579606
if (execResp.isSetQueryId()) {
580607
queryId = execResp.getQueryId();
581608
}

0 commit comments

Comments
 (0)