Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Question about Spring transaction propagation

I have a question about Spring transaction propagation.

Suppose I use @Transactional(propagation = Propagation.REQUIRED) to annotate a method m1(). When execution logic enters m1(), if there is already a transaction, m1() will use that one. When after m1(), what about the transaction? It will end or still remain open? (if I call m1() in another method, and after the invocation there is still other things to do).

In summary, I want to know when exiting an annotated method, the transaction ends or still remains open?

Great thanks.

like image 659
Just a learner Avatar asked May 02 '10 16:05

Just a learner


People also ask

What is propagation required in Spring transaction?

The REQUIRED propagation can be interpreted as follows: If there is no existing physical transaction, then the Spring container will create one. If there is an existing physical transaction, then the methods annotated with REQUIRE will participate in this physical transaction.

How does the transaction propagation setting impact the behavior of transactions?

Transaction Propagation - MANDATORYIf none exists then gets executed with out transaction. Always executes in a transaction. If there is any existing transaction it is used. If there is no existing transaction it will throw an exception.

Which approaches for transaction management are supported in Spring?

Spring supports both programmatic and declarative transaction management.


1 Answers

Propagation.REQUIRED (documented here) will create a new transaction (if none exists for the current thread), or will join an existing transaction (if one exists).

When the method exits, then the transaction will be completed (if entering the method caused a transaction to be created), or will leave the transaction open (if a transaction already existed when the method was entered). In other, words, it's symmetrical, and will leave the thread's transactional state in the same state it was before the method was entered.

like image 78
skaffman Avatar answered Nov 16 '22 01:11

skaffman