3

I am studying how to Spring handle transaction and I am asking what happens if one @Transactional annotated method is calling another @Transactional annotated method on the same object instance?

I know that the transactional propagation defaul level is REQUIRED so if I have a method1() annotated with @Transactional of an instance of MyObject that call a method2() (annotated with @Transactional) of the same instance I think that, following the REQUIRED level, it is performed on the same transaction created by method1().

Is it my reasoning correct or am I missing something? I am not sure about what exactly happens if the 2 @Transactional methods are called on the same instance.

How it works?

Ian Lim
  • 4,164
  • 3
  • 29
  • 43

1 Answers1

16

If you call method2() from method1() within the same class, the @Transactional annotation of the second method will not have any effect because it is not called through proxy, but directly. Methods are enhanced with transactional behavior only if called through proxy (autowired bean, or some instance injected in any other way).

But generally speaking, if method1() and method2() were in different classes, and both were annotated with @Transactional (so using REQUIRED propagation), then they would share the same transaction started in method1().

Predrag Maric
  • 23,938
  • 5
  • 52
  • 68