@EnableTransactionManagement turns on Spring's annotation-driven transaction management, so methods marked @Transactional run inside a transaction that Spring begins, commits or rolls back for you. In Spring Boot you rarely need to write it, because TransactionAutoConfiguration enables it as soon as a transaction manager bean exists. You declare it explicitly in plain Spring applications or when you want to change the proxy mode.
Understanding how it works, through proxies, explains most surprising transaction behavior.
@TransactionalThe annotation registers an interceptor and an advisor. Spring wraps each bean that has @Transactional methods in a proxy. When code calls a method through the proxy, the interceptor asks the PlatformTransactionManager to start (or join) a transaction, calls the real method, then commits on success or rolls back on failure.
Common transaction managers are JpaTransactionManager for JPA, DataSourceTransactionManager for JDBC, JmsTransactionManager and KafkaTransactionManager.
Boot detects the transaction manager that matches your starter (for example JpaTransactionManager with Spring Data JPA) and enables annotation-driven transactions automatically. Adding @EnableTransactionManagement on top is harmless but redundant. By default Boot also sets spring.aop.proxy-target-class=true, which means class-based (CGLIB) proxies rather than interface-based JDK proxies.
@TransactionalIf withdraw throws an unchecked exception, both changes roll back.
@EnableTransactionManagement has three notable attributes:
| Attribute | Default | Effect |
|---|---|---|
mode | PROXY | ASPECTJ uses compile-time or load-time weaving instead of proxies |
proxyTargetClass | false | true creates class-based proxies |
order | lowest precedence | Position of the transaction advisor among other advisors |
@Transactional itself controls propagation, isolation, timeout, readOnly, rollbackFor and noRollbackFor.
RuntimeException and Error). For a checked exception use @Transactional(rollbackFor = Exception.class).a() calls this.b() and only b() is transactional, no transaction starts. Call it through another bean.readOnly = true is a hint for the transaction manager and the persistence provider; it does not forbid writes in every database.@Async methods, runs outside the caller's transaction.Annotate a Spring test with @Transactional and the framework rolls the test transaction back at the end, keeping the database clean. Remember that this differs from production behavior if your code relies on the commit happening.
@EnableTransactionManagement in Spring Boot?No, in most cases. Boot auto-configures it when a transaction manager is available.
@Transactional method not rolling back?Typical reasons are a checked exception, a self-invocation, a non-public method, or an exception caught inside the method.
@Transactional should I import?Use org.springframework.transaction.annotation.Transactional. The Jakarta version jakarta.transaction.Transactional is also supported but offers fewer options.
Proxy mode works through runtime proxies and misses self-calls. AspectJ weaving changes the bytecode, so self-calls and non-public methods are covered too.
@EnableTransactionManagement enables proxy-based, annotation-driven transactions for @Transactional methods. Spring Boot turns it on automatically, so you add it by hand mainly in plain Spring or to set the proxy mode. Keep the proxy rules in mind: public methods, calls from other beans and unchecked exceptions by default.