102

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?

Alexis Dufrenoy
  • 11,784
  • 12
  • 82
  • 124
  • 7
    See this link: http://forum.springsource.org/archive/index.php/t-16594.html -- Juergen Hoeller explain it very well – Ralph Sep 12 '12 at 14:51
  • @Ralph: thank you, that's exactly what I was looking for. You should add it as an answer. – Alexis Dufrenoy Sep 12 '12 at 15:02
  • 1
    @Ralph : great, that would be a best answer. – Nandkumar Tekale Sep 12 '12 at 15:04
  • 1
    So the main difference is that with a nested transaction policy, the transaction can be rollbacked to the beginning to the current atomic operation, which is the same as in a requires_new policy, but it will only be commited at the end of the whole process, which is completly different from the requires_new policy, where each atomic operation will be commited when it ends. – Alexis Dufrenoy Sep 12 '12 at 15:04
  • IMHO, This difference is clearer actually. But REQUIRED and NESTED ones which are behaved similar each other are confusing. They have some differences about ability like savePoints. – webyildirim May 20 '16 at 08:45
  • 2
    @Ralph unfortunately, your link no longer points to an existing page :( – knittl May 17 '20 at 13:27

3 Answers3

142

See this link: PROPAGATION_NESTED versus PROPAGATION_REQUIRES_NEW? Juergen Hoeller explain it very well. -- the Spring Source Forum is completely offline since 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. ...

veinhorn
  • 2,363
  • 6
  • 21
  • 33
Ralph
  • 118,862
  • 56
  • 287
  • 383
  • 1
    Good answer and good question. Your comments and the following link were very useful for me: http://www.byteslounge.com/tutorials/spring-transaction-propagation-tutorial – yaki_nuka Jun 23 '14 at 14:35
  • is it the right behavior that you described in propagation_requires_new? cause as I checked it rollbacks both the transaction. – eatSleepCode Oct 16 '15 at 05:39
  • please clarify outer transaction behaviour if nested transaction fails(will it fails too ?) and vice versa – gstackoverflow Jan 10 '17 at 13:19
  • So with nested transaction, when the inner one rollbacks, the outer one continues at the save point whereas when the outer one rollbacks, all action are rolled back, right? – Wecherowski Jul 19 '17 at 11:02
  • Any differences in behaviour? – gstackoverflow Oct 21 '18 at 18:12
  • 1
    This Spring reference explains it in quite detail: https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/data-access.html#tx-propagation – bluelurker Oct 23 '20 at 11:24
20

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

Nandkumar Tekale
  • 16,024
  • 8
  • 58
  • 85
  • 3
    Yes, I understand the underlining difference, but I can't see how it will behave differently: In one case, I will rollback a to the previous savepoint, in the other, I will rollback the current transaction and not the outer one, but actually, in both cases I will rollback to the beginning of the current atomic operation and start again from that point. – Alexis Dufrenoy Sep 12 '12 at 14:59
  • 1
    @Traroth : The two lines on the link by @Ralph would tell the difference in behavior. `PROPAGATION_REQUIRES_NEW` :The outer transaction will get suspended at the beginning of the inner one, and resumed once the inner one has completed. Each inner transaction committed/rolledback when it is completed. `PROPAGATION_NESTED` : The nested transaction is part of of the outer transaction, so it will only be committed at the end of of the outer transaction. – Nandkumar Tekale Sep 12 '12 at 15:15
  • I agree, so I suggested him to make an answer out of his comment. – Alexis Dufrenoy Sep 12 '12 at 15:28
  • 1
    @Traroth : I agree with you. :) He has the best answer. – Nandkumar Tekale Sep 12 '12 at 15:31
-5

Please find the difference

1.) Use of NESTED Transaction

Execute within a nested transaction if a current transaction exists, behave like PROPAGATION_REQUIRED else. Nested transaction is supporting by Spring

2.)Use of REQUIRED Transaction Support a current transaction, create a new one if none exists. . It means for banking domain like withdraw,deposit, update the transaction

3.) Use of REQUIRES_NEW Transaction Create a new transaction, and suspend the current transaction if one exists.

Ankit
  • 593
  • 4
  • 12