@EnableTransactionManagement in Spring Boot

后端 未结 6 1776
你的背包
你的背包 2020-12-07 16:58

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 s

相关标签:
6条回答
  • 2020-12-07 17:01

    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"

    0 讨论(0)
  • 2020-12-07 17:05

    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.

    0 讨论(0)
  • 2020-12-07 17:06

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

    0 讨论(0)
  • 2020-12-07 17:09

    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)

    0 讨论(0)
  • 2020-12-07 17:10

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

    0 讨论(0)
  • 2020-12-07 17:10

    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.

    0 讨论(0)
提交回复
热议问题