Senior
Method one @Transactional calls method two @Transactional(REQUIRES_NEW) in the same class. How many transactions will be created?
sobes.tech AI
Answer from AI
If a method is annotated with @Transactional with the default type (usually REQUIRED), and within it, a method two with @Transactional(REQUIRES_NEW) is called, then two transactions will be created:
- The first transaction — for method one.
- The second transaction — for method two, which will be independent and will start at the moment method two is called, while the first transaction will be suspended.
However, if both methods are in the same class and the call to method two occurs directly (without using Spring proxy), then the @Transactional(REQUIRES_NEW) annotation will not work, and the transaction will not be recreated. In this case, only one transaction will be used.
For REQUIRES_NEW to work correctly, the call must go through a Spring proxy, for example, via a call from another bean or through self-injection.