Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what exactly "Propagation.REQUIRES_NEW" means using Spring transaction management?

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?

like image 806
meisam Avatar asked Apr 26 '11 11:04

meisam


People also ask

What is propagation Requires_new?

When the propagation is REQUIRES_NEW, Spring suspends the current transaction if it exists, and then creates a new one: @Transactional(propagation = Propagation.

What is propagation in Spring transaction?

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.

What happens when a method annotated with @transaction propagation Requires_new is invoked?

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.

How does transaction management work in Spring?

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.


1 Answers

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.

like image 145
Bozho Avatar answered Jan 01 '23 21:01

Bozho