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 methoda(), 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()andb(), and assuming that the default propagation behavior (Propagation.REQUIRED) is used, the following will happen:When
a()is invoked, a new transaction (associated withtmA) will be started due to the@Transactionalannotation on methoda().Within the scope of the transaction started by
a(), when methodb()is called, a new transaction (associated withtmB) will be started due to the@Transactionalannotation on methodb(). This is because each method's transactional settings are handled independently, and thePropagation.REQUIREDbehavior starts a new transaction if one does not already exist.Method
b()will be executed within the context of the new transaction associated withtmB.Once method
b()completes, its transaction will be committed, and the transaction associated withtmA(from methoda()) will continue.Finally, when method
a()completes, the transaction associated withtmAwill be committed as well.In summary, when method
b()is called from methoda(), a new transaction will be started - like @M. Deinum has commented - for methodb()due to the different transaction managers, and it will not continue with the transaction from methoda().