This is an automated email from the ASF dual-hosted git repository.

JackieTien97 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/iotdb.git


The following commit(s) were added to refs/heads/master by this push:
     new 383458fb72c Fix JDBC reusing stale statement id after reconnect 
(#18078)
383458fb72c is described below

commit 383458fb72c255de67fe3520df3b10492017cdc2
Author: Jackie Tien <[email protected]>
AuthorDate: Wed Jul 1 14:04:33 2026 +0800

    Fix JDBC reusing stale statement id after reconnect (#18078)
---
 .../java/org/apache/iotdb/jdbc/IoTDBStatement.java | 35 +++++++++++++++++++---
 1 file changed, 31 insertions(+), 4 deletions(-)

diff --git 
a/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBStatement.java 
b/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBStatement.java
index 1c6ce8c0e9b..17fe5ef3617 100644
--- a/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBStatement.java
+++ b/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBStatement.java
@@ -363,7 +363,14 @@ public class IoTDBStatement implements Statement {
     execReq.setTimeout((long) queryTimeout * 1000);
     TSExecuteStatementResp execResp =
         callWithRetryAndReconnect(
-            () -> client.executeStatementV2(execReq), 
TSExecuteStatementResp::getStatus);
+            () -> {
+              // reConnect() may have replaced the session/statement id, so 
refresh them on every
+              // attempt; otherwise the server reports "StatementId doesn't 
exist in this session".
+              execReq.setSessionId(sessionId);
+              execReq.setStatementId(stmtId);
+              return client.executeStatementV2(execReq);
+            },
+            TSExecuteStatementResp::getStatus);
 
     if (execResp.isSetOperationType() && 
execResp.getOperationType().equals("dropDB")) {
       connection.changeDefaultDatabase(null);
@@ -430,7 +437,13 @@ public class IoTDBStatement implements Statement {
     isCancelled = false;
     TSExecuteBatchStatementReq execReq = new 
TSExecuteBatchStatementReq(sessionId, batchSQLList);
     TSStatus execResp =
-        callWithRetryAndReconnect(() -> client.executeBatchStatement(execReq), 
status -> status);
+        callWithRetryAndReconnect(
+            () -> {
+              // reConnect() may have replaced the session id, so refresh it 
on every attempt.
+              execReq.setSessionId(sessionId);
+              return client.executeBatchStatement(execReq);
+            },
+            status -> status);
     int[] result = new int[batchSQLList.size()];
     boolean allSuccess = true;
     StringBuilder message = new StringBuilder(System.lineSeparator());
@@ -500,7 +513,14 @@ public class IoTDBStatement implements Statement {
     execReq.setJdbcQuery(true);
     TSExecuteStatementResp execResp =
         callWithRetryAndReconnect(
-            () -> client.executeQueryStatementV2(execReq), 
TSExecuteStatementResp::getStatus);
+            () -> {
+              // reConnect() may have replaced the session/statement id, so 
refresh them on every
+              // attempt; otherwise the server reports "StatementId doesn't 
exist in this session".
+              execReq.setSessionId(sessionId);
+              execReq.setStatementId(stmtId);
+              return client.executeQueryStatementV2(execReq);
+            },
+            TSExecuteStatementResp::getStatus);
     queryId = execResp.getQueryId();
     try {
       RpcUtils.verifySuccess(execResp.getStatus());
@@ -575,7 +595,14 @@ public class IoTDBStatement implements Statement {
     final TSExecuteStatementReq execReq = new TSExecuteStatementReq(sessionId, 
sql, stmtId);
     final TSExecuteStatementResp execResp =
         callWithRetryAndReconnect(
-            () -> client.executeUpdateStatement(execReq), 
TSExecuteStatementResp::getStatus);
+            () -> {
+              // reConnect() may have replaced the session/statement id, so 
refresh them on every
+              // attempt; otherwise the server reports "StatementId doesn't 
exist in this session".
+              execReq.setSessionId(sessionId);
+              execReq.setStatementId(stmtId);
+              return client.executeUpdateStatement(execReq);
+            },
+            TSExecuteStatementResp::getStatus);
     if (execResp.isSetQueryId()) {
       queryId = execResp.getQueryId();
     }

Reply via email to