Understanding Spring Transaction Management: @EnableTransactionManagement, Propagation, and Synchronization
This article explains how Spring enables transaction management with @EnableTransactionManagement, details the underlying beans and proxy mechanisms, describes the basic execution flow, explores transaction propagation scenarios, shows how to force rollbacks, and demonstrates using TransactionSynchronization for lifecycle callbacks.
Spring transaction management is activated by the @EnableTransactionManagement annotation, which registers two essential beans: AutoProxyRegistrar and ProxyTransactionManagementConfiguration .
AutoProxyRegistrar adds an InfrastructureAdvisorAutoProxyCreator bean to the container; this creator extends AbstractAdvisorAutoProxyCreator and automatically generates proxy objects for beans that have the @Transactional annotation.
ProxyTransactionManagementConfiguration defines three additional beans: BeanFactoryTransactionAttributeSourceAdvisor (an Advisor), AnnotationTransactionAttributeSource (the Pointcut that detects @Transactional on classes or methods), and TransactionInterceptor (the Advice that contains the transaction logic).
When a method annotated with @Transactional is invoked, TransactionInterceptor uses the configured PlatformTransactionManager to create a database connection, set autocommit to false, execute the business method, and then either commit or roll back the transaction based on whether an exception was thrown.
The propagation mechanism determines how nested method calls behave. For example, when method a() calls b() , Spring checks the current thread’s ThreadLocal for an existing connection. If a transaction already exists, it may join it, create a new one, or suspend the existing transaction depending on the propagation setting (e.g., REQUIRED , REQUIRES_NEW , etc.). The article presents four concrete cases illustrating normal commit, rollback due to an exception, and the suspend‑resume process when a new transaction is required.
To force a rollback even when an exception is caught, developers can call TransactionAspectSupport.currentTransactionStatus().setRollbackOnly() inside the catch block.
Spring also provides TransactionSynchronization , allowing developers to register callbacks for transaction lifecycle events such as suspend , resume , beforeCommit , beforeCompletion , afterCommit , and afterCompletion . The following example shows how to register a TransactionSynchronization implementation within a @Transactional method:
@Component
public class UserService {
@Autowired
private JdbcTemplate jdbcTemplate;
@Autowired
private UserService userService;
@Transactional
public void test() {
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
@Override
public void suspend() { System.out.println("test被挂起了"); }
@Override
public void resume() { System.out.println("test被恢复了"); }
@Override
public void beforeCommit(boolean readOnly) { System.out.println("test准备要提交了"); }
@Override
public void beforeCompletion() { System.out.println("test准备要提交或回滚了"); }
@Override
public void afterCommit() { System.out.println("test提交成功了"); }
@Override
public void afterCompletion(int status) { System.out.println("test提交或回滚成功了"); }
});
jdbcTemplate.execute("insert into t1 values(1,1,1,1,'1')");
userService.a();
}
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void a() {
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
@Override
public void suspend() { System.out.println("a被挂起了"); }
@Override
public void resume() { System.out.println("a被恢复了"); }
@Override
public void beforeCommit(boolean readOnly) { System.out.println("a准备要提交了"); }
@Override
public void beforeCompletion() { System.out.println("a准备要提交或回滚了"); }
@Override
public void afterCommit() { System.out.println("a提交成功了"); }
@Override
public void afterCompletion(int status) { System.out.println("a提交或回滚成功了"); }
});
jdbcTemplate.execute("insert into t1 values(2,2,2,2,'2')");
}
}Through these mechanisms, Spring provides fine‑grained control over transaction creation, propagation, forced rollback, and lifecycle monitoring, enabling robust data consistency handling in backend Java applications.
IT Architects Alliance
Discussion and exchange on system, internet, large‑scale distributed, high‑availability, and high‑performance architectures, as well as big data, machine learning, AI, and architecture adjustments with internet technologies. Includes real‑world large‑scale architecture case studies. Open to architects who have ideas and enjoy sharing.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.