Fix Statement.execute() skipping update count after batch statement error #2866
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
When executing a mixed batch of SQL statements using Statement.execute() (e.g. INSERT → INSERT error → INSERT → SELECT), if a statement in the middle of the batch fails (such as a primary key violation) and the application catches the resulting SQLException and calls getMoreResults() to continue, the driver incorrectly skips the update count of the next valid DML statement.
As a result, the update count for a successful statement following an error is never returned, and result processing jumps directly from the error to the subsequent ResultSet. This violates expected JDBC result traversal semantics and causes applications to lose valid update counts.
This issue is tracked in : Statement.execute() skips valid update count after catching SQLException in mixed batch execution #2850
Fix
The issue was caused by incorrect handling of DONE tokens for failed statements during result traversal.
Specifically, the driver skipped DONE tokens with updateCount = -1 without checking whether the token represented an error.
The fix updates the parsing logic to not skip error DONE tokens, ensuring the parser stops at the correct error position in the TDS stream. This allows getMoreResults() to correctly advance to the next statement and expose the update count of subsequent successful DML operations.
This change prevents valid update counts from being swallowed after an error in mixed batch execution.
Testing
Added a new test that executes a mixed SQL batch containing:
The test fails with the previous behavior and passes with this fix applied.