Let's say I have two methods a() in class A and b() in class B. Both methods a() and b() are annotated with @Transactional annotation but with different transaction managers. Every TM uses different datasource which in fact connects to the same db with the same credentials.
What happens when the the method b() is called from method a(). Does the transaction continue with the transaction from from a() or does it start a new transaction?
OSIV disabled.
class A {
private B b;
@Transactional(transactionManager = "tmA")
public void a() {
b.b();
}
}
class B {
@Transactional(transactionManager = "tmB")
public void b() {}
}
I didn't find similar question.
When method b()
is called from method a()
, the behavior depends on the transaction propagation settings and the fact that Open Session in View is disabled.
Since you are using different transaction managers for methods a()
and b()
, and assuming that the default propagation behavior (Propagation.REQUIRED
) is used, the following will happen:
When a()
is invoked, a new transaction (associated with tmA
) will be started due to the @Transactional
annotation on method a()
.
Within the scope of the transaction started by a()
, when method b()
is called, a new transaction (associated with tmB
) will be started due to the @Transactional
annotation on method b()
. This is because each method's transactional settings are handled independently, and the Propagation.REQUIRED
behavior starts a new transaction if one does not already exist.
Method b()
will be executed within the context of the new transaction associated with tmB
.
Once method b()
completes, its transaction will be committed, and the transaction associated with tmA
(from method a()
) will continue.
Finally, when method a()
completes, the transaction associated with tmA
will be committed as well.
In summary, when method b()
is called from method a()
, a new transaction will be started - like @M. Deinum has commented - for method b()
due to the different transaction managers, and it will not continue with the transaction from method a()
.