Reputation: 23
I would like to understand when exactly commit happens and when exactly rollback happens in case of nested transaction with different isolation levels to calling and called methods,
For example, I have two spring services and i'm calling method2 of service2 from method1 of service1.
Method1 is having REQUIRED transaction scope and Method2 is having REQUIRES_NEW transaction scope as shown in the program below.
Service1 {
@Transactional(propagation = Propagation.REQUIRED)
method1()
{
for(int i=0; i<10; i++){
service2.method2();
}
// Some more code which takes some time to process
}
}
Service2 {
@Transactional(propagation = Propagation.REQUIRES_NEW)
method2()
{
//Save some information to DB
// Save an object using JPA
}
}
Now my question is, As i understand REQUIRES_NEW will start a new transaction, But will it commit immediately on existing method2 or will the spring waits till method1 is completed and then commits?
I'm interested in at what point of time commit happens and row lock in DB gets released which is persisted in method2.
Note: Here i have placed both methods in different services so that spring can achieve nested transaction.
Thanks in Advance,
Vali
Upvotes: 2
Views: 5055
Reputation: 446
When you enter the method2 of service2, the transaction of the service1 (say tx1
) get suspended, and a new transaction is created to service2 (say tx2
). This new transaction is independent of the previous transaction, and will either commit or rollback independently.
tx2 will commit/rollback just when you return from the service2, and after that tx1 will resume from the point it suspended. The result of tx2 (whether it resulted in a commit or a rollback) will not affect the behavior of tx1.
Please read the Spring documentation. Check section 16.5.7 for more information about transaction propagation.
Upvotes: 5