fix: JDBC TransactionScope is not cleared on completion#2594
Open
SIMULATAN wants to merge 1 commit intoJetBrains:mainfrom
Open
fix: JDBC TransactionScope is not cleared on completion#2594SIMULATAN wants to merge 1 commit intoJetBrains:mainfrom
SIMULATAN wants to merge 1 commit intoJetBrains:mainfrom
Conversation
Related: KTOR-8747
Author
|
Ideally, I'd like to backport this fix to the 0.x.y train as the 1.0.0 migration is quite the undertaking. However, as 1.x is in Beta already, I'd (reluctantly) understand if you decide against that. In case you're open to merging a backport, please let me know which branch to target. |
SIMULATAN
added a commit
to SnotyMe/snoty-backend
that referenced
this pull request
Aug 24, 2025
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
Summary of the change: Clear the Transaction from the
TransactionManager'sThreadLocalafter completion in order to prevent retrieving closed Transactions throughTransactionManager#currentOrNullDetailed description:
Since JDBC Transactions are not cleared from the TransactionManager's ThreadLocal after completion, future calls to
TransactionManager#currentOrNullfrom the same Thread results in a closed transaction being returned, leading to leaking transactions, which ultimately clears the entire thread pool of usable connections, rendering the entire application unable to make any Database calls.I've created SIMULATAN/ktor-exposed-leak-reproducer to demonstrate the issue. Instructions can be found in the README.
The R2DBC equivalent to the
withTransactionScopefunction correctly cleans the context up. My patch aligns the JDBC implementation with the R2DBC one.This issue originally surfaced through KTOR-8747. The change made in this PR fixes that issue, although I'd label the new persistence of ThreadLocals between requests as a regression, therefore prompting further investigation on the Ktor side to prevent additional side effects.
Type of Change
Updates/remove existing public API methods:
Affected databases:
Checklist
Related Issues
https://youtrack.jetbrains.com/issue/KTOR-8747/Netty-Exposed-suspending-transactions-leak-since-3.2.0