You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
TransactionSynchronizationManager - throw an Exception or log a warning if a Synchronization wants to add a Synchronization and afterCompletion is already called [SPR-11590] #16214
i've got a service A which registered a TransactionSynchronization to do stuff in the afterCommit() case.
It calls a service B to index things to lucene.
I've forgot that i did already leveraged the transaction handling to the lucene service B, so i did wonder why nothing happened and no error was thrown.
After debugging i've found that A did register a Synchronization (doAfterCommit) to call B, and B did also register one (doAfterCompletion).
The one which B registered was never called because doAfterCommit is the last trigger to be called and triggerAfterCompletion was already run.
Maybe a check can be added for addSynchronization(), that it throws an Exception, if the afterCompletion trigger is already reached - or at least a log warning would be nice.
This is a more general problem that we have also seen.
Transaction synchronizations are very convenient, so convenient in fact that we've had bugs where code that is running within a synchronization invokes some other code which invokes some other code which itself registers a synchronization. This is a classic programmer "booby trap" that is easy to fall into. The result is that the latter synchronization never runs, because it's too late (the PlatformTransactionManager has already grabbed a snapshot of the list of synchronizations and is currently working through that list).
This problem is exacerbated by the fact that TransactionSynchronizationManager.isSynchronizationActive() returns true even while synchronization callbacks are being invoked. So any code that asks the question "Can I do this in an after-commit callback?" gets a "yes" answer, even though the answer is really "no".
What's worse is that the failure mode is for the callback to be silently ignored.
This should all be tightened up, both in specification and implementation. In other words, re-entrant synchronization registration behavior is not documented (it should be), and the (undocumented) behavior is sub-optimal (it should be improved).
My recommendation:
Add a new configuration variable to AbstractPlatformTransactionManager called enforceStrictCallbacks or whatever, default false.
Create a new package-private, thread-local boolean in TransactionSynchronizationManager called disableCallbacks or whatever.
If someone invokes TransactionSynchronizationManager.registerSynchronization() when TransactionSynchronizationManager.disableCallbacks is set to true, throw an IllegalStateException.
When AbstractPlatformTransactionManager.enforceStrictCallbacks is false, do the current behavior (for backward compatibility).
However, when AbstractPlatformTransactionManager.enforceStrictCallbacks is true, have AbstractPlatformTransactionManager set TransactionSynchronizationManager.disableCallbacks for the duration of its callback processing.
Document all of this in the AbstractPlatformTransactionManager and TransactionSynchronizationManager API javadoc.
AbstractPlatformTransactionManager clears TransactionSynchronizationManager's synchronizations right before triggering afterCompletion now, with TransactionSynchronizationManager.isSynchronizationActive() returning false at that point and registerSynchronization therefore naturally rejecting any further synchronizations.
AbstractPlatformTransactionManager clears TransactionSynchronizationManager's synchronizations right before triggering afterCompletion now, with TransactionSynchronizationManager.isSynchronizationActive() returning false at that point and registerSynchronization therefore naturally rejecting any further synchronizations.
@jhoeller
I don't understand why do you useTransactionSynchronizationManager.clearSynchronization(); to clear synchronizations, which causing #isSynchronizationActive returning false and #isActualTransactionActive returning true.
Someone may use #isActualTransactionActive to determine if current thread is in a transaction.
Could you please tell me why don't use TransactionSynchronizationManager.clear() which making #isSynchronizationActive and #isActualTransactionActive both return false ?
Activity
spring-projects-issues commentedon Oct 13, 2016
Archie Cobbs commented
This is a more general problem that we have also seen.
Transaction synchronizations are very convenient, so convenient in fact that we've had bugs where code that is running within a synchronization invokes some other code which invokes some other code which itself registers a synchronization. This is a classic programmer "booby trap" that is easy to fall into. The result is that the latter synchronization never runs, because it's too late (the
PlatformTransactionManagerhas already grabbed a snapshot of the list of synchronizations and is currently working through that list).This problem is exacerbated by the fact that
TransactionSynchronizationManager.isSynchronizationActive()returns true even while synchronization callbacks are being invoked. So any code that asks the question "Can I do this in an after-commit callback?" gets a "yes" answer, even though the answer is really "no".What's worse is that the failure mode is for the callback to be silently ignored.
This should all be tightened up, both in specification and implementation. In other words, re-entrant synchronization registration behavior is not documented (it should be), and the (undocumented) behavior is sub-optimal (it should be improved).
My recommendation:
AbstractPlatformTransactionManagercalledenforceStrictCallbacksor whatever, default false.booleaninTransactionSynchronizationManagercalleddisableCallbacksor whatever.TransactionSynchronizationManager.registerSynchronization()whenTransactionSynchronizationManager.disableCallbacksis set to true, throw anIllegalStateException.AbstractPlatformTransactionManager.enforceStrictCallbacksis false, do the current behavior (for backward compatibility).AbstractPlatformTransactionManager.enforceStrictCallbacksis true, haveAbstractPlatformTransactionManagersetTransactionSynchronizationManager.disableCallbacksfor the duration of its callback processing.AbstractPlatformTransactionManagerandTransactionSynchronizationManagerAPI javadoc.spring-projects-issues commentedon Feb 9, 2017
Juergen Hoeller commented
AbstractPlatformTransactionManagerclearsTransactionSynchronizationManager's synchronizations right before triggeringafterCompletionnow, withTransactionSynchronizationManager.isSynchronizationActive()returningfalseat that point andregisterSynchronizationtherefore naturally rejecting any further synchronizations.chenxi-null commentedon Oct 18, 2019
@jhoeller
I don't understand why do you use
TransactionSynchronizationManager.clearSynchronization();to clear synchronizations, which causing#isSynchronizationActivereturning false and#isActualTransactionActivereturning true.Someone may use
#isActualTransactionActiveto determine if current thread is in a transaction.Could you please tell me why don't use
TransactionSynchronizationManager.clear()which making#isSynchronizationActiveand#isActualTransactionActiveboth return false ?Thanks.