Is @EnableTransactionManagement
required in Spring Boot?
I did some research. Some folks say you don't need it, as Spring Boot has it already enabled, others say you do have to use it explicitly. So how is it?

- 22,894
- 45
- 188
- 319

- 4,136
- 10
- 36
- 53
6 Answers
Probably you're also using Spring Data. Calls on Spring Data repositories are by default surrounded by a transaction, even without @EnableTransactionManagement
. If Spring Data finds an existing transaction, the existing transaction will be re-used, otherwise a new transaction is created.
@Transactional
annotations within your own code, however, are only evaluated when you have @EnableTransactionManagement
activated (or configured transaction handling some other way).
You can easily trace transaction behavior by adding the following property to your application.properties
:
logging.level.org.springframework.transaction.interceptor=TRACE

- 22,894
- 45
- 188
- 319

- 3,913
- 19
- 28
According to > https://spring.io/guides/gs/managing-transactions/
Spring Boot will detect spring-jdbc on the classpath and h2 and will create a DataSource and a JdbcTemplate for you automatically. Because such infrastructure is now available and you have no dedicated configuration, a DataSourceTransactionManager will also be created for you: this is the component that intercepts the @Transactional annotated method.
You can also use spring-boot-starter-actuator to list your beans created in your context and you will find it
bean": "transactionManager"

- 541
- 3
- 4
-
11So the answer is: If you have spring-jdbc on your classpath, you don't need @EnableTransactionManagement, right? – Dirk Apr 06 '18 at 09:28
-
2only if you have @EnableAutoConfiguration in your configuration class though. Otherwise it will not work. – kekko12 Aug 24 '19 at 13:25
-
12@kekko12 `@EnableAutoConfiguration is added by @SpringBootApplication` – Alex78191 Mar 23 '20 at 13:35
Little old post but the answers given previously were not straight forward when I was searching for it.
@EnableTransactionManagement
is optional in Spring boot, provided that spring-data* or spring-tx are found in classpath. How it works? As below:
Spring boot adds a spring-boot-autoconfigure.jar in the classpath. Go to the META-INF's spring.factories file and you can see org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration
entry there. This initializes the transaction auto configuration for you.
Note that the class has following lines: (snippet)
@Configuration
@ConditionalOnClass({PlatformTransactionManager.class})
@AutoConfigureAfter({JtaAutoConfiguration.class, HibernateJpaAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class, Neo4jDataAutoConfiguration.class})
@EnableConfigurationProperties({TransactionProperties.class})
public class TransactionAutoConfiguration {
..
}
Have a look at TransactionAutoConfiguration
to see that it enables transaction support if the PlatformTransactionManager
is available in classpath. EnableTransactionManagementConfiguration
is also configured there.
-
`only if you have @EnableAutoConfiguration in your configuration class though` So it's not true? – Alex78191 Oct 07 '19 at 18:47
-
9@Alex78191 `@EnableAutoConfiguration` is added by `@SpringBootApplication` – v.ladynev Mar 22 '20 at 22:02
No. @EnableTransactionManagement
is on by default, see that: https://github.com/jkubrynski/spring-boot/commit/9d219ef7a004c58a88bbbef82a520a22961c9402

- 4,176
- 7
- 47
- 81

- 351
- 1
- 3
- 8
@EnableTransactionManagement is conditionally turned on/off based of the dependency jars we add in the classpath. If we use spring data jpa starter it is turned on.

- 84
- 1
- 1
In the class org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration
, there is such code(Spring Boot 1.5+):
@Configuration
@EnableTransactionManagement(proxyTargetClass = false)
@ConditionalOnProperty(prefix = "spring.aop", name = "proxy-target-class", havingValue = "false", matchIfMissing = false)
public static class JdkDynamicAutoProxyConfiguration {
}
@Configuration
@EnableTransactionManagement(proxyTargetClass = true)
@ConditionalOnProperty(prefix = "spring.aop", name = "proxy-target-class", havingValue = "true", matchIfMissing = true)
public static class CglibAutoProxyConfiguration {
}
The default is spring.aop.proxy-target-class=true
, enabling CGLIB proxy by default.
If you want to use JDK proxy, set spring.aop.proxy-target-class=false
instead.