my confusion related to this matter is that how we can use a previously created transaction? or in other words how many threads a transaction could be related to?
When the propagation is REQUIRES_NEW, Spring suspends the current transaction if it exists, and then creates a new one: @Transactional(propagation = Propagation.
Propagation is the ability to decide how the business methods should be encapsulated in both logical or physical transactions. Spring REQUIRED behavior means that the same transaction will be used if there is an already opened transaction in the current bean method execution context.
The main difference between them is if a method in Spring Business Activity/DAO class is annotated with Propagation. REQUIRES_NEW, then when the execution comes to this method, it will create a new transaction irrespective of the existing transaction whereas if the method is annotated with Propagation.
Transactions and Proxies. At a high level, Spring creates proxies for all the classes annotated with @Transactional, either on the class or on any of the methods. The proxy allows the framework to inject transactional logic before and after the running method, mainly for starting and committing the transaction.
A transaction can be related to only one thread in spring. Well, with some effort you can make it a long-running transaction, but that's an anti-pattern afaik.
REQUIRES_NEW
means that whenever the program flow enters the annotated method, a new transaction will be started regardless of any existing transaction.
REQUIRED
means that an existing transaction will be reused, or if there's no existing transaction a new one will be started.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With