@EnableTransactionManagement switches on annotation-driven transaction management. It registers the infrastructure that detects @Transactional on beans and wraps them in proxies, so each annotated method starts a transaction, commits on success and rolls back on failure.
In a Spring Boot application you normally do not need to write it, because Boot's transaction auto-configuration applies it for you. In plain Spring, or when you want to change the proxy mode, you add it to a @Configuration class together with a transaction manager bean.
The annotation is the Java-config equivalent of the XML element <tx:annotation-driven/>. It imports a selector that registers three things:
TransactionAttributeSource, which reads @Transactional settings from classes and methods.TransactionInterceptor, the advice that begins, commits or rolls back the transaction.@Transactional method.It also needs a TransactionManager bean to do the real work: JpaTransactionManager for JPA, DataSourceTransactionManager for plain JDBC, or a ReactiveTransactionManager for reactive stacks.
Both updates run in one transaction. If credit throws an unchecked exception, the debit is undone.
| Attribute | Default | Meaning |
|---|---|---|
mode | AdviceMode.PROXY | PROXY uses Spring AOP proxies; ASPECTJ weaves the advice into bytecode |
proxyTargetClass | false | true forces CGLIB class proxies instead of JDK interface proxies |
order | lowest precedence | Position of the transaction advice relative to other advisors |
Spring Boot sets proxyTargetClass to true by default, controlled by spring.aop.proxy-target-class. Use ASPECTJ mode when you need self-invocation or non-public methods to be transactional without limits, at the cost of build-time or load-time weaving.
Boot's TransactionAutoConfiguration enables transaction management whenever a TransactionManager bean exists, and spring-boot-starter-data-jpa or spring-boot-starter-jdbc provides one. Adding a second @EnableTransactionManagement is harmless but redundant. You only need it explicitly when you disable auto-configuration or build a non-Boot application.
A RuntimeException or Error triggers rollback. A checked exception does not, unless you ask for it.
With the default proxy mode, the transaction logic lives in the proxy. A call from one method to another method in the same class goes straight to this, so the @Transactional on the target method is ignored. Move the method to another bean, or use AspectJ mode.
Since Spring 6, class-based (CGLIB) proxies also honour @Transactional on protected and package-visible methods. Interface-based JDK proxies still only see methods declared on the interface. final classes and methods cannot be proxied by CGLIB, so the annotation has no effect on them.
When more than one TransactionManager bean exists, name the one to use: @Transactional("ordersTx"). Otherwise Spring cannot pick a default and fails with a NoUniqueBeanDefinitionException.
Set readOnly = true for query-only methods, which lets some drivers and JPA providers optimise. Use propagation to control how nested calls join a transaction, and isolation to choose the isolation level.
No. Boot enables it automatically when a transaction manager is on the classpath. Add it only for non-Boot setups or when auto-configuration is turned off.
The usual causes are a checked exception without rollbackFor, a self-invocation that skips the proxy, an exception caught and swallowed inside the method, or a bean that Spring does not manage.
PROXY wraps beans at runtime and only intercepts calls coming from outside the bean. ASPECTJ weaves the behaviour into the compiled classes, so internal calls are also transactional, but it needs a weaver.
Yes. With a ReactiveTransactionManager such as the R2DBC one, @Transactional applies to methods returning Mono or Flux, and the transaction is bound to the reactive pipeline instead of a thread.
@EnableTransactionManagement activates @Transactional by registering the interceptor and advisor that wrap your beans in transactional proxies. Spring Boot does this for you; plain Spring needs the annotation plus a transaction manager bean. Remember the default rollback rule, the self-invocation limit of proxies, and use rollbackFor or AspectJ mode when those defaults do not fit.