Inconsistent Transaction and TransactionSynchronizationManager Attributes with Nested Transactions in Multi-Transaction Manager Environment #35039
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.
Hi !
situation
In a multi-transaction manager environment, when
@Transactional
annotations from different transaction managers are nested, the attributes of TransactionSynchronizationManager do not align with the current transaction if the inner@Transactional
method has a propagation level ofSUPPORTS
,NOT_SUPPORTS
, orNEVER
.Specifically, TransactionSynchronizationManager inherits the attributes (e.g., readOnly status) of the outer/parent's
other transaction manager
, rather than reflecting the characteristics of the "empty" or "non-participating" transaction created by the inner@Transactional
method under these propagation settings. This leads to a mismatch between the expected transaction state and the state reported by TransactionSynchronizationManager.Steps to Reproduce
Consider the following simplified example:
Expected Behavior
When transactionSecond() is invoked,
TransactionSynchronizationManager.isCurrentTransactionReadOnly()
should accurately reflect the characteristics of the current transaction context established by manager2's @transactional, irrespective of any parent transaction's attributes.This is especially critical when the parent transaction originates from a different transaction manager. While it might seem that
PROPAGATION_SUPPORTS
,PROPAGATION_NOT_SUPPORTED
, orPROPAGATION_NEVER
shouldn't be affected as they don't create a new transaction,@Transactional
attributes like readOnly can be set regardless of whether a physical transaction is actually active. Therefore, these attributes should be applied and reflected independently.Indeed, if
@Transactional(transactionManager = "manager2", readOnly = false, propagation = Propagation.SUPPORTS)
were to execute without a parent transaction,TransactionSynchronizationManager.isCurrentTransactionReadOnly()
would correctly return false. This demonstrates that@Transactional
attributes should be precisely reflected, regardless of the presence of other transaction managers.Therefore, whether
transactionFirst()
is invoked ortransactionSecond()
is called directly,assert TransactionSynchronizationManager.isCurrentTransactionReadOnly()
should not throw an exception.Actual Behavior
However, when
transactionFirst()
is invoked, anAssertionError
is thrown whentransactionSecond()
is called. Conversely, iftransactionSecond()
is invoked directly, noAssertionError
occurs.Proposed Changes
In AbstractPlatformTransactionManager.getTransaction(Definition), we've added suspend logic at the very end of the method. This ensures that when a transaction context is established with propagations like
PROPAGATION_SUPPORTS
,PROPAGATION_NOT_SUPPORTED
, orPROPAGATION_NEVER
, any previously active transaction (potentially from another TransactionManager) is correctly suspended. This guarantees that TransactionSynchronizationManager accurately reflects the characteristics of the current transaction context, preventing inconsistencies in readOnly status and other attributes.