Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@EnableTransactionManagement in Spring Boot

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?

like image 450
jarosik Avatar asked Nov 21 '16 15:11

jarosik


People also ask

Is @EnableTransactionManagement required in Spring boot?

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

What is the use of @EnableTransactionManagement annotation?

Annotation Type EnableTransactionManagement. Enables Spring's annotation-driven transaction management capability, similar to the support found in Spring's <tx:*> XML namespace. To be used on @Configuration classes to configure traditional, imperative transaction management or reactive transaction management.

What is the use of @transactional annotation?

The @Transactional annotation makes use of the attributes rollbackFor or rollbackForClassName to rollback the transactions, and the attributes noRollbackFor or noRollbackForClassName to avoid rollback on listed exceptions. The default rollback behavior in the declarative approach will rollback on runtime exceptions.

What does @transactional do in Spring?

At a high level, when a class declares @Transactional on itself or its members, Spring creates a proxy that implements the same interface(s) as the class you're annotating. In other words, Spring wraps the bean in the proxy and the bean itself has no knowledge of it.


2 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 

(see Showing a Spring transaction in log)

like image 55
Tom Avatar answered Sep 21 '22 13:09

Tom


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"

like image 44
mmgc84 Avatar answered Sep 25 '22 13:09

mmgc84