Skip to content

Fix JDBC reusing stale statement id after reconnect#18078

Merged
JackieTien97 merged 1 commit into
masterfrom
fix-jdbc-reconnect-stale-statement-id
Jul 1, 2026
Merged

Fix JDBC reusing stale statement id after reconnect#18078
JackieTien97 merged 1 commit into
masterfrom
fix-jdbc-reconnect-stale-statement-id

Conversation

@JackieTien97

@JackieTien97 JackieTien97 commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

Problem

When IoTDBStatement.reConnect() runs (e.g. a DataNode is restarted mid-query), this.sessionId and this.stmtId are refreshed — but the retry lambda still sends the originally-built TSExecuteStatementReq carrying the old session/statement ids.

On the server side, ClientRPCServiceImpl.executeStatementInternal resolves the session from the current RPC connection (getCurrSessionAndUpdateIdleTime()) while taking the statementId from the request body. After reconnect that yields new-session + old-statement-id, so ClientSession.addQueryId throws:

StatementId: N doesn't exist in this session

which is mapped to INTERNAL_SERVER_ERROR (305). This is the root cause of the recurring query_restart_dn_show_dev JDBC test failures: the test's check_log_INTERNAL_SERVER_ERROR greps the 305s and fails, even though the query results themselves are correct.

Fix

Refresh sessionId (and statementId) on the request object inside every retry lambda, mirroring SessionConnection in the Java Session SDK, which already handles this correctly.

Affected methods in IoTDBStatement:

  • executeSQL (executeStatementV2) — sessionId + statementId
  • executeQuerySQL (executeQueryStatementV2) — sessionId + statementId
  • executeUpdateSQL (executeUpdateStatement) — sessionId + statementId
  • executeBatchSQL (executeBatchStatement) — sessionId (batch has no statement id)

Verification

  • mvn spotless:apply -pl iotdb-client/jdbc — clean
  • mvn compile -pl iotdb-client/jdbc — BUILD SUCCESS

After reConnect() refreshes this.sessionId/this.stmtId, the retry lambda in
executeSQL/executeQuerySQL/executeUpdateSQL still sends the original
TSExecuteStatementReq carrying the OLD ids. The server resolves the session
by the current RPC connection (NEW session) but takes the statementId from
the request body (OLD), so ClientSession.addQueryId throws
"StatementId ... doesn't exist in this session", surfaced as
INTERNAL_SERVER_ERROR(305) -- the root cause of recurring
query-restart-datanode JDBC test failures.

Refresh sessionId and statementId inside each retry lambda, mirroring
SessionConnection in the Session SDK, which already does this correctly.
executeBatchSQL is refreshed for sessionId for consistency (no statement id).
@sonarqubecloud

sonarqubecloud Bot commented Jul 1, 2026

Copy link
Copy Markdown

@codecov

codecov Bot commented Jul 1, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 76.92308% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 41.60%. Comparing base (127eff0) to head (a76f4f7).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
...ain/java/org/apache/iotdb/jdbc/IoTDBStatement.java 76.92% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             master   #18078    +/-   ##
==========================================
  Coverage     41.59%   41.60%            
  Complexity      318      318            
==========================================
  Files          5294     5294            
  Lines        371228   371414   +186     
  Branches      48036    48059    +23     
==========================================
+ Hits         154413   154521   +108     
- Misses       216815   216893    +78     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@JackieTien97 JackieTien97 merged commit 383458f into master Jul 1, 2026
42 of 43 checks passed
@JackieTien97 JackieTien97 deleted the fix-jdbc-reconnect-stale-statement-id branch July 1, 2026 06:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant