Reputation: 4564
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?
Upvotes: 94
Views: 144847
Reputation: 4093
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
(see Showing a Spring transaction in log)
Upvotes: 104
Reputation: 381
No. @EnableTransactionManagement
is on by default, see that: https://github.com/jkubrynski/spring-boot/commit/9d219ef7a004c58a88bbbef82a520a22961c9402
Upvotes: 17
Reputation: 94
@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.
Upvotes: 6
Reputation: 5786
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.
Upvotes: 37
Reputation:
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.
Upvotes: 4
Reputation: 571
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"
Upvotes: 46