Fix update count handling for multi-statement queries executed via PreparedStatement. (#2722) - #2737
Merged
Merged
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #2737 +/- ##
=======================================
Coverage ? 51.54%
Complexity ? 4071
=======================================
Files ? 149
Lines ? 34244
Branches ? 5719
=======================================
Hits ? 17650
Misses ? 14104
Partials ? 2490 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Mahendra Chavan (machavan)
previously approved these changes
Aug 12, 2025
Muskan Gupta (muskan124947)
previously approved these changes
Aug 12, 2025
Divang Sharma (divang)
previously approved these changes
Aug 12, 2025
David Engel (David-Engel)
left a comment
Collaborator
There was a problem hiding this comment.
Just the changes around unnecessary catching of exceptions in new tests. Otherwise approved.
Ananya Garg (Ananya2)
dismissed stale reviews from Divang Sharma (divang), Muskan Gupta (muskan124947), and Mahendra Chavan (machavan)
via
August 13, 2025 04:49
17707a5
Divang Sharma (divang)
approved these changes
Aug 13, 2025
Muskan Gupta (muskan124947)
approved these changes
Aug 13, 2025
Mahendra Chavan (machavan)
approved these changes
Aug 13, 2025
5 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
This PR addresses an inconsistency in how PreparedStatement handles update counts for multi-statement queries, particularly when INSERT operations are involved.
Issue
When executing multi-statement queries containing INSERT operations, PreparedStatement returned different update counts compared to Statement due to differences in Tabular Data Stream (TDS) token processing:
Example of inconsistent behavior:
PreparedStatement ps = conn.prepareStatement( "DELETE FROM TestTable; " + "INSERT INTO TestTable (c1,c2) VALUES (?, ?); " + "INSERT INTO TestTable (c1,c2) VALUES (?, ?); " + "UPDATE TestTable SET C1 = 1; " + "INSERT INTO TestTable (c1,c2) VALUES (?, ?); " + "SELECT * FROM TestTable" ); do { boolean hasResults = ps.getMoreResults(); System.out.println("update count: " + ps.getUpdateCount() + ", has result: " + hasResults); } while (true);update count: 3, has result :false update count: 1, has result :false update count: 1, has result :false update count: 2, has result :false update count: 1, has result :false update count: -1, has result :trueupdate count: 3, has result :false update count: 2, has result :false update count: 1, has result :false update count: -1, has result :trueRoot Cause
In PR #2554, the following logic was added to handle INSERT operations:
if ((StreamDone.CMD_INSERT == doneToken.getCurCmd()) && (-1 != doneToken.getUpdateCount()) && EXECUTE == executeMethod) { return true; }This logic is correct for Statement but not for PreparedStatement.
For PreparedStatement, the execute API for INSERT does not require reading an additional explicit TDS_DONE token — applying the same logic caused incorrect update counts.
Fix
Added overridden methods to correctly handle update count retrieval in both PreparedStatement and SQLServerStatement, ensuring that each uses the appropriate TDS token processing logic.
Testing
Closes #2722