Skip to content

Conversation

Ananya2
Copy link
Contributor

@Ananya2 Ananya2 commented Aug 19, 2025

Fix PreparedStatement getGeneratedKeys() returning "statement must be executed" error when triggers are present

Problem:

  • PreparedStatement.getGeneratedKeys() was failing with "The statement must be executed before any results can be obtained" when INSERT statements had triggers

Root Cause:

  • PR #2737 correctly made SQLServerPreparedStatement.hasUpdateCountTDSTokenForInsertCmd() return false to fix update count issues for multi-statement queries
  • However, this change did not account for the generated keys use case where bRequestedGeneratedKeys is true
  • When hasUpdateCountTDSTokenForInsertCmd() returns false for PreparedStatement, it affects the TDS token processing in such a way that the generated keys ResultSet is not properly consumed or made available
  • When triggers fire and generated keys are requested, additional TDS tokens must be processed to locate the ResultSet containing the generated keys

Solution:

  • Modified hasUpdateCountTDSTokenForInsertCmd() to return true when generated keys are requested (bRequestedGeneratedKeys = true)
  • This maintains the correct update count behavior for multi-statement queries while preserving generated keys functionality
  • The conditional approach ensures both scenarios work correctly without breaking existing functionality

Testing:

Fixes #2740

@Ananya2 Ananya2 changed the title Fix PreparedStatement getGeneratedKeys() returning "statement must be executed" error when triggers are present (#2740) Fix PreparedStatement getGeneratedKeys() failure with insert triggers (#2740) Aug 19, 2025
@Ananya2 Ananya2 self-assigned this Aug 19, 2025
Copy link

codecov bot commented Aug 19, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 51.74%. Comparing base (e1acc9e) to head (22ea5e2).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@             Coverage Diff              @@
##               main    #2742      +/-   ##
============================================
+ Coverage     51.51%   51.74%   +0.22%     
- Complexity     4081     4141      +60     
============================================
  Files           149      149              
  Lines         34268    34268              
  Branches       5725     5725              
============================================
+ Hits          17654    17731      +77     
+ Misses        14125    14110      -15     
+ Partials       2489     2427      -62     

☔ View full report in Codecov by Sentry.
📢 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.

@machavan machavan added this to the 13.2.1 milestone Aug 19, 2025
@Ananya2 Ananya2 merged commit 6a39b6e into main Aug 29, 2025
19 checks passed
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.

Generated keys no longer returned if there is a trigger on insert
3 participants