Spring and threads – transactions

The Spring Framework offers an extensive API for database transaction management. Spring takes care of all basic transaction management control and provides a consistent programming model for different transaction APIs, such as JDBC, Hibernate, Java Transaction API (JTA), Java Persistence API (JPA), and Java Data Objects (JDO). There are two types of transactions provided by Spring: one is declarative and the other is programmatic transaction management. Declarative is very high-level, while programmatic is more advanced but flexible.

Spring transaction management works very well with a single thread. But it cannot manage a transaction across multiple threads. If we try to use the transaction with multiple threads, our program gives a runtime error or an unexpected result.

To understand why a Spring transaction fails with multiple threads, first, we need to understand how transactions work with Spring. Spring stores all transaction information in the ThreadLocal variables inside the org.springframework.transaction.support.TransactionSynchronizationManager class:

public abstract class TransactionSynchronizationManager {
private static final Log logger =
LogFactory.getLog(TransactionSynchronizationManager.class);
private static final ThreadLocal<Map<Object, Object>> resources = new
NamedThreadLocal("Transactional resources");
private static final ThreadLocal<Set<TransactionSynchronization>>
synchronizations = new NamedThreadLocal("Transaction
synchronizations");
private static final ThreadLocal<String> currentTransactionName = new
NamedThreadLocal("Current transaction name");
private static final ThreadLocal<Boolean> currentTransactionReadOnly
= new NamedThreadLocal("Current transaction read-only status");
private static final ThreadLocal<Integer>
currentTransactionIsolationLevel = new NamedThreadLocal("Current
transaction isolation level");
private static final ThreadLocal<Boolean> actualTransactionActive =
new NamedThreadLocal("Actual transaction active");
}

The thread's local variable holds the information of a specific transaction for a single thread only and that cannot be accessed by another thread. So, an ongoing transaction's information is not passed to the newly created thread. The result will be an error indicating that the transaction is missing.

Now we are able to understand the problem of a Spring transaction with multiple threads. Spring cannot maintain the transaction state to old threads from newly created threads. To solve the transaction problem with multiple threads, we need to manually pass the thread's local variable values to the newly created thread.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset