The annotation @Transactional in Spring Boot


Introduce

The @Transactional annotation is part of the Spring Framework and is used to manage transactions declaratively. It can be applied to classes or methods in your service layer.


Article content

@Transactional under the hook.

The image depicts that the transactional bound context will applied to all methods in this class. When we call the method annotated by @Transactional by default it will be wrapped within an TransactionAspectSupport class and the actual method is executed in invokeWithinTransaction method (We can use this to check if we are configure transaction in our application correctly).

Article content

  • In a transaction, a connection is reused across multiple database access object (DAO) method calls. So when we have a limited connection pool and process a business for a long time we can shortly run out of database connection if the business is called frequently.
  • Depending on propagation we define in @Transactional the code at line 225 will reuse the existing transaction or create a new one.
  • If we mixed use JdbcTemplate and JPA does @Transactional bounded context still apply? The answer is yes. In the below image the update invoke within TransactionAspectSupport

Article content


Key to takeaways

  • In Spring Data JPA, when we only want to retrieve data without modifying it we should use @Transactional(readOnly=true). Since the JPA doesn't need to keep track state of objects.

Article content

Luckily, the spring framework does the job for us to make SimpleJpaRepository (which is the parent class of JpaRepository we usually use) annotated with read-only transactions.

  • By default mode of Spring AOP the call directly to a @Transactional annotated methods of the same class will not work.

Article content

Article content

With this setup, the @Transactional we write is useless since we can't find any TransactionAspectSupport in the call stack.


Hoang Tuan (Daniel) Minh

⚡ AI Engineer | FE Developer | ReactJS | Database Optimization

9mo

Very helpful!

Like
Reply
Quoc Manh Trinh

⚡️Software Developer

9mo

Thanks for sharing

Like
Reply
Nam Nguyen

⚡ Culi developer Chia sẻ về những điều mình biết, những sai lầm đã gặp phải và giải pháp luôn :D Whatever you are not changing, you are choosing Let's connect!

9mo

great post. It make me remember 1 question I got when do interview: Have you ever faced the problem when your @transaction called another service with @transaction? What happens if the inner transaction falls back? At first, he used the term transaction propagation and I have no idea what's it

Nguyen Trung Nam

Senior Software Architect @ VPS Securities JSC

9mo

Cảm ơn bạn, bài viết rất hay và dễ hiểu. Đầy đủ các trường hợp và lưu ý khi sử dụng. Việc sử dụng @Transactional thì AOP sẽ tạo ra proxy class, bản thân method trong class gọi nhau sẽ k đi qua lớp proxy này. Các annotation khác cũng như thế. Nói chung bài viết rất thực tế và áp dụng được ngay và luôn.

To view or add a comment, sign in

More articles by Lê Quốc Dũng

Insights from the community

Others also viewed

Explore topics