Announcement Announcement Module
Collapse
No announcement yet.
PROPAGATION_NESTED versus PROPAGATION_REQUIRES_NEW? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • PROPAGATION_NESTED versus PROPAGATION_REQUIRES_NEW?

    Would someone please describe the difference between these two TransactionDefinitions? I'm most interested in what happens to a pre-existing transaction, when/if it is rolled back, etc.

  • #2
    Take a look at TransactionDefinition. Where there's a name match, the transaction terms have the same meaning as their EJB CMT counterparts.

    Would someone please describe the difference between these two TransactionDefinitions?
    PROPAGATION_REQUIRES_NEW will suspend the current transaction (if one esists) and start a new one. They are independent and a rollback shouldn't affect the first transaction.

    PROPAGATION_NESTED will start a nested transaction (if one already exists, otherwise it behaves like PROPAGATION_REQUIRED). I haven't used this, but I expect you can rollback either the nested transaction, or both the nested and containing transaction. The Tx manager (e.g. DataSourceTransactionManager) must use the JDBC 3.0 Savepoint API (and obviously the DB and driver must support nested transations).

    Comment


    • #3
      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.

      Such independent inner transactions are for example used for id generation through manual sequences, where the access to the sequence table should happen in its own transactions, to keep the lock there as short as possible. The goal there is to avoid tying the sequence locks to the (potentially much longer running) outer transaction, with the sequence lock not getting released before completion of the outer transaction.

      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.

      Nested transactions essentially allow to try some execution subpaths as subtransactions: rolling back to the state at the beginning of the failed subpath, continuing with another subpath or with the main execution path there - all within one isolated transaction, and not losing any previous work done within the outer transaction.

      For example, consider parsing a very large input file consisting of account transfer blocks: The entire file should essentially be parsed within one transaction, with one single commit at the end. But if a block fails, its transfers need to be rolled back, writing a failure marker somewhere. You could either start over the entire transaction every time a block fails, remembering which blocks to skip - or you mark each block as a nested transaction, only rolling back that specific set of operations, keeping the previous work of the outer transaction. The latter is of course much more efficient, in particular when a block at the end of the file fails.

      Juergen

      Comment


      • #4
        Originally posted by Juergen Hoeller
        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.
        Makes perfect sense.

        Is there any way that the nested transaction can roll back the entire transaction, beyond the created savepoint?

        Comment


        • #5
          Rolling back the entire transaction is the choice of the demarcation code/config that started the outer transaction.

          So if an inner transaction throws an exception and is supposed to be rolled back (according to the rollback rules), the transaction will get rolled back to the savepoint taken at the start of the inner transaction. The immediate calling code can then decide to catch the exception and proceed down some other path within the outer transaction.

          If the code that called the inner transaction lets the exception propagate up the call chain, the exception will eventually reach the demarcation code of the outer transaction. At that point, the rollback rules of the outer transaction decide whether to trigger a rollback. That would be a rollback of the entire outer transaction then.

          So essentially, it depends on your exception handling. If you catch the exception thrown by the inner transaction, you can proceed down some other path within the outer transaction. If you let the exception propagate up the call chain, it's eventually gonna cause a rollback of the entire outer transaction.

          Juergen

          Comment

          Working...
          X