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() ?
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.
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).
A flat or nested transaction that accesses objects handled by different servers is referred to as a distributed transaction.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With