Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested @Transactional

Tags:

Is it possible to nest @Transactional annotated methods in spring? Consider something like this:

@Transactional public void a() {     obj.b(); }  @Transactional public void b() {     // ...  } 

What happens in such a case if I rollback in b() and rollback in a() ?

like image 617
Erik Avatar asked Jul 09 '11 09:07

Erik


People also ask

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

Definition. A nested transaction model as proposed by Moss is a generalization of the flat transaction model that allows nesting. A nested transaction forms a tree of transactions with the root being called a top-level transaction and all other nodes called nested transactions (subtransactions).

Can nested transaction be distributed?

A flat or nested transaction that accesses objects handled by different servers is referred to as a distributed transaction.


1 Answers

The second @Transactional annotation on method b() is not required because by default @Transactional has a propagation of REQUIRED, therefore methods called by method a() will be transactional. If you are looking to start a new transaction within a method called by method a() you will need to modify the propagation rules. Read about Transaction Propagation.

like image 133
Kevin Bowersox Avatar answered Sep 21 '22 05:09

Kevin Bowersox