Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differences between requires_new and nested propagation in Spring transactions

I can't understand the behavior difference between the PROPAGATION_REQUIRES_NEW and PROPAGATION_NESTED propagation policies. It seems to me that in both cases, the current process is rollbacked but not the whole transaction. Any clue?

like image 295
Alexis Dufrenoy Avatar asked Sep 12 '12 14:09

Alexis Dufrenoy


People also ask

What is the difference between required and Requires_new?

REQUIRED, spring checks for any existing transaction. If yes, it uses that old transaction otherwise it creates a new one. But, the disadvantage of Propagation. REQUIRES_NEW is that even if the inner method fails to execute (because of some exception), the outer method commits the transaction.

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 nested transaction Spring?

Spring nested transactions are a bit like the black sheep of the transaction family – easily misunderstood and cast aside in documentation. Nested transactions are however incredibly useful for use cases consisting of large bulk updates and/or operating over unreliable connections.

What is @transactional propagation propagation required?

Propagation. REQUIRED is the default setting of a @Transactional annotation. The REQUIRED propagation can be interpreted as follows: If there is no existing physical transaction, then the Spring container will create one.


2 Answers

See this link: PROPAGATION_NESTED versus PROPAGATION_REQUIRES_NEW? Juergen Hoeller explain it very well. -- the Spring Source Forum is completely offline sice February 28, 2019, but you can read the relevant part of the article in the quote below

PROPAGATION_REQUIRES_NEW starts a new, independent "inner" transaction for the given scope. This transaction will be committed or rolled back completely independent from the outer transaction, having its own isolation scope, its own set of locks, etc. The outer transaction will get suspended at the beginning of the inner one, and resumed once the inner one has completed. ...

PROPAGATION_NESTED on the other hand starts a "nested" transaction, which is a true subtransaction of the existing one. What will happen is that a savepoint will be taken at the start of the nested transaction. Íf the nested transaction fails, we will roll back to that savepoint. The nested transaction is part of of the outer transaction, so it will only be committed at the end of of the outer transaction. ...

like image 154
Ralph Avatar answered Sep 20 '22 23:09

Ralph


PROPAGATION_REQUIRES_NEW : uses a completely independent transaction for each affected transaction scope. In that case, the underlying physical transactions are different and hence can commit or roll back independently, with an outer transaction not affected by an inner transaction's rollback status.

PROPAGATION_NESTED : uses a single physical transaction with multiple savepoints that it can roll back to. Such partial rollbacks allow an inner transaction scope to trigger a rollback for its scope, with the outer transaction being able to continue the physical transaction despite some operations having been rolled back. This setting is typically mapped onto JDBC savepoints, so will only work with JDBC resource transactions.

check spring documentation

like image 30
Nandkumar Tekale Avatar answered Sep 19 '22 23:09

Nandkumar Tekale