@EnableTransactionManagement in Spring Boot

Spring BootTransactions

Spring Boot Problem Overview


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?

Spring Boot Solutions


Solution 1 - Spring Boot

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 https://stackoverflow.com/questions/1965454/showing-a-spring-transaction-in-log)

Solution 2 - Spring Boot

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"

Solution 3 - Spring Boot

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.

Solution 4 - Spring Boot

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

Solution 5 - Spring Boot

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

Solution 6 - Spring Boot

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjarosikView Question on Stackoverflow
Solution 1 - Spring BootTomView Answer on Stackoverflow
Solution 2 - Spring Bootmmgc84View Answer on Stackoverflow
Solution 3 - Spring BootKarthik RView Answer on Stackoverflow
Solution 4 - Spring BootWilliam HeView Answer on Stackoverflow
Solution 5 - Spring BootMahesh VarmaView Answer on Stackoverflow
Solution 6 - Spring Bootuser10313453View Answer on Stackoverflow