Skip to content

fix: clear the transaction flag when COMMIT or ROLLBACK fails - #6249

Open
adityaanikam wants to merge 1 commit into
SQLMesh:mainfrom
adityaanikam:fix-commit-failure-clears-transaction-flag-6248
Open

adityaanikam wants to merge 1 commit into
SQLMesh:mainfrom
adityaanikam:fix-commit-failure-clears-transaction-flag-6248

Conversation

@adityaanikam

@adityaanikam adityaanikam commented Nov 24, 2026 •

Copy link
Copy Markdown

Description

Fixes #6248.

commit() and rollback() on the connection pools cleared the "transaction active" flag only after the database call succeeded. When COMMIT failed (Redshift's serializable isolation violation in the issue), the flag stayed set, so every later EngineAdapter.transaction() on that thread skipped BEGIN and COMMIT and its writes were never committed.

The flag is now cleared in a finally in both pool classes, for commit() and for rollback(), which has the same shape.

Test Plan

Added test_connection_pool_failed_commit_or_rollback_ends_transaction for the three pool classes. Six of its cases fail without the change. I also ran tests/core/engine_adapter/test_base.py together with tests/utils/test_connection_pool.py (136 passed) and ruff check / ruff format --check on the two files.

Checklist

  • I have run make style and fixed any issues
  • I have added tests for my changes (if applicable)
  • All existing tests pass (make fast-test)
  • My commits are signed off (git commit -s) per the DCO

Summary by CodeRabbit

  • Bug Fixes
    • Connection pools now clear transaction state after commit or rollback attempts, including when an operation fails. Failures continue to be reported to the caller.
  • Tests
    • Added coverage for commit and rollback failures across connection pool types.

Signed-off-by: adityaanikam <adityanikam9502@gmail.com>
Copilot AI balanced review requested due to automatic review settings November 24, 2026 10:37

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@coderabbitai

coderabbitai Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Connection pools now clear transaction state after commit and rollback attempts, including when those operations raise. Parameterized tests cover both outcomes across three pool classes.

Changes: Pool Transaction State

Layer / File(s) Summary
Clear state after transaction operations
sqlmesh/utils/connection_pool.py, tests/utils/test_connection_pool.py
Thread-local and singleton pools clear transaction state after commit and rollback attempts. Parameterized tests verify that operation failures propagate and leave no transaction active.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: High

Suggested reviewers: stuffbyyuki

Merge Risk: Minimal · up to 75501

A failed COMMIT or ROLLBACK no longer leaves the pool believing a transaction is still active. Later transactions on that thread, or on the whole adapter for the singleton pool, can begin and commit normally. The change is small and well covered by tests. Database-level recovery after a failed commit was not verified against a real Redshift connection.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: the transaction-active flag is now cleared when COMMIT or ROLLBACK fails. It matches the raw summary, which says commit() and rollback() clear state in finally blocks.…
Description check Passed The description states the problem, the fix, and the test plan. It covers the linked issue #6248 and the Redshift failure. The checklist is filled in for tests and DCO sign-off. Two template items are…
Linked Issues check Passed [#6248] The requirement is that a failed COMMIT must not leave the connection pool's transaction-active flag set, so later transactions issue BEGIN and COMMIT normally. The diff to sqlmesh/utils/conne…
Out of Scope Changes check Passed All changes are in sqlmesh/utils/connection_pool.py and tests/utils/test_connection_pool.py. The pool changes directly implement the flag-clearing requirement of #6248. The test additions verify that …
Full details: Title check

Explanation

The title names the main change: the transaction-active flag is now cleared when COMMIT or ROLLBACK fails. It matches the raw summary, which says commit() and rollback() clear state in finally blocks. It follows the fix: prefix convention and is specific and concise.

Full details: Description check

Explanation

The description states the problem, the fix, and the test plan. It covers the linked issue #6248 and the Redshift failure. The checklist is filled in for tests and DCO sign-off. Two template items are unchecked: make style and make fast-test. The author states ruff check and ruff format were run, but make fast-test was not run, and the checklist does not say so. The template's Test Plan section is present and populated.

Full details: Linked Issues check

Explanation

[#6248] The requirement is that a failed COMMIT must not leave the connection pool's transaction-active flag set, so later transactions issue BEGIN and COMMIT normally. The diff to sqlmesh/utils/connection_pool.py wraps _do_commit() and _do_rollback() in try/finally in _ThreadLocalBase (via _discard_transaction(get_ident())) and in SingletonConnectionPool (via _is_transaction_active = False). The flag is therefore cleared when the database call raises, and the exception still propagates to EngineAdapter.transaction(). The added parameterized test covers commit and rollback failures for all three pool classes and asserts that no transaction remains active. The issue's reproduction (second transaction after a failed commit should end with COMMIT) is addressed by this change. The issue's suggested fix also says to attempt a rollback after a failed commit. The diff does not add that call. The issue describes this as a suggested fix, and its stated expected behavior is met without it, so this gap does not fail the requirement. The issue's Redshift serialization violation itself is out of scope, as the issue states.

Full details: Out of Scope Changes check

Explanation

All changes are in sqlmesh/utils/connection_pool.py and tests/utils/test_connection_pool.py. The pool changes directly implement the flag-clearing requirement of #6248. The test additions verify that behavior. The diff shows no unrelated code changes. The raw summary lists only these two files, and ignored_files is empty.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: c328ce5c-d1c9-4153-8f99-4b7959199cf5
Commits

Reviewing files that changed from the base of the PR and between 660fdef and 755016e.

Files selected for processing: 2

  • sqlmesh/utils/connection_pool.py
  • tests/utils/test_connection_pool.py

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

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.

Failed COMMIT leaves the connection pool's transaction flag set, so later batches are never committed (Redshift backfill hangs)

2 participants