79

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?

Koray Tugay
  • 22,894
  • 45
  • 188
  • 319
jarosik
  • 4,136
  • 10
  • 36
  • 53

6 Answers6

94

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)

Koray Tugay
  • 22,894
  • 45
  • 188
  • 319
Tom
  • 3,913
  • 19
  • 28
43

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"

mmgc84
  • 541
  • 3
  • 4
  • 11
    So the answer is: If you have spring-jdbc on your classpath, you don't need @EnableTransactionManagement, right? – Dirk Apr 06 '18 at 09:28
  • 2
    only 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
31

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.

stkent
  • 19,772
  • 14
  • 85
  • 111
Karthik R
  • 5,523
  • 2
  • 18
  • 30
15

No. @EnableTransactionManagement is on by default, see that: https://github.com/jkubrynski/spring-boot/commit/9d219ef7a004c58a88bbbef82a520a22961c9402

Martin Schröder
  • 4,176
  • 7
  • 47
  • 81
William He
  • 351
  • 1
  • 3
  • 8
5

@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.

Mahesh Varma
  • 84
  • 1
  • 1
4

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.