-2

In many articles I read the following "Spring's declarative transaction is enabled with AOP proxies".

For a newbie like me, what does that mean exactly ?

On what kind of classes or beans can I use the @Transactional annotation ?

Do I have to add the maven dependency "spring-aop" in POM.xml ?

Is @EnableTransactionManagement explicitely needed in order to make transaction work ?

user2023141
  • 895
  • 5
  • 17
  • 36
  • Does https://stackoverflow.com/questions/1099025/spring-transactional-what-happens-in-background answer your question? – M. Deinum Apr 16 '21 at 06:59
  • 1
    Yes, this answers most of my questions. – user2023141 Apr 16 '21 at 07:04
  • Does this answer your question? [Spring - @Transactional - What happens in background?](https://stackoverflow.com/questions/1099025/spring-transactional-what-happens-in-background) – aheze Apr 19 '21 at 15:44

1 Answers1

0

There is already an answer which goes into a bit of detail on what @Transactional does and how it works.

Regarding your other questions.

On what kind of classes or beans can I use the @Transactional annotation?

In theory on each class you want. Should you do this no. For instance, making your web layer, the transactional boundary is generally considered as being a bad idea, as it is your service layer that should be the transactional boundary.

Do I have to add the maven dependency spring-aop in pom.xml ?

No. The spring-tx dependency already pulls this in itself. When using the spring-boot-starter-data-jpa (or another persistence related one) it will also be included automatically.

Is @EnableTransactionManagement explicitly needed in order to make the transaction work?

It depends. Generally speaking in a Spring Boot application you won't need to add this, as Spring Boot will automatically enable this. If you don't use Spring Boot but just a regular Spring application you will need to add @EnableTransactionManagement or <tx:annotation-driven> when using XML to tell Spring to process the @Transactional annotations.

M. Deinum
  • 115,695
  • 22
  • 220
  • 224
  • Can the @Transactional annotation be used on any class ? For example, on the application I have to maintain, the Transactional annotation is used on a Controller class (class annotated with RestController). And we often have the following exception : " javax.persistence.TransactionRequiredException: Executing an update/delete query " – user2023141 Apr 16 '21 at 07:32
  • In theory you can, should you no. Annotating your controller is generally a bad idea as the service layer is your transactional boundary, not your web layer. – M. Deinum Apr 16 '21 at 07:33
  • So, it means that the TransactionRequiredException is not due to the fact that DB access is done in a Controller class, right ? – user2023141 Apr 16 '21 at 07:37