I am a little bit confused about transaction resource management in Spring. Namely, I am confused about the usage of TransactionSynchronizationManager.isActualTransactionActive
and TransactionSynchronizationManager.isSynchronizationActive
.
Up to now, probably incorrectly, I assumed that isSynchronizationActive
was used (also within the Spring codebase) to figure out whether there is an active transaction, initiated by TransactionSynchronizationManager.initSynchronization()
. As far as I am concerned, when we suspend a transaction, the actual isSynchronizationActive
is still true! I presume, therefore, the correct way of establishing a running transaction is by using isActualTransactionActive
, correct?
If this is the case, what is the actual point of isSynchronizationActive
method? I understand it tells you whether you can add synchronizations or not, but I am a bit lost about what it tells us about the transaction...
public abstract class TransactionSynchronizationManager extends Object. Central delegate that manages resources and transaction synchronizations per thread. To be used by resource management code but not by typical application code.
Spring provides support for synchronizing resources with transactions since the earliest versions. We often use it to synchronize transactions managed by multiple transaction managers. For example, we can synchronize a JMS commit with a JDBC commit.
However, if we're using a Spring Boot project and have a spring-data-* or spring-tx dependencies on the classpath, then transaction management will be enabled by default.
You will notice the following fields of TransactionSynchronizationManager
private static final ThreadLocal<Set<TransactionSynchronization>> synchronizations =
new NamedThreadLocal<Set<TransactionSynchronization>>("Transaction synchronizations");
private static final ThreadLocal<Boolean> actualTransactionActive =
new NamedThreadLocal<Boolean>("Actual transaction active");
and the methods
public static boolean isSynchronizationActive() {
return (synchronizations.get() != null);
}
public static boolean isActualTransactionActive() {
return (actualTransactionActive.get() != null);
}
The TransactionSynchronizationManager
basically acts as a registry for TransactionSynchronization
. The javadoc states
If transaction synchronization isn't active, there is either no current transaction, or the transaction manager doesn't support transaction synchronization.
So you first init and register TransactionSynchronization
with initSynchronization()
and registerSynchronization(TransactionSynchronization)
. When these are registered, the TransactionManager
can start a Transaction
and tell the TransactionSynchronizationManager
if it's active or not with setActualTransactionActive(boolean)
.
In conclusion, isSynchronizationActive()
tells us if TransactionSynchronization
has been enabled, even if no TransactionSynchronization
instances have been registered.
isActualTransactionActive()
tells us if there is an actual Transaction
object active.
The TransactionSynchronizationManager
javadoc states
Central helper that manages resources and transaction synchronizations per thread. To be used by resource management code but not by typical application code.
so don't ignore it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With