[英]Spring transactions: Method with requires_new called from a method with requires
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.例如,我有两个 spring 服务,我从 service1 的 method1 调用 service2 的 method2。
Method1 is having REQUIRED transaction scope and Method2 is having REQUIRES_NEW transaction scope as shown in the program below. Method1 具有 REQUIRED 事务范围,Method2 具有 REQUIRES_NEW 事务范围,如下面的程序所示。
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?现在我的问题是,据我所知,REQUIRES_NEW 将启动一个新事务,但是它会在现有的 method2 上立即提交还是会等到 method1 完成然后提交?
I'm interested in at what point of time commit happens and row lock in DB gets released which is persisted in method2.我感兴趣的是在什么时间点提交发生并且 DB 中的行锁被释放,这在方法 2 中持久化。
Note: Here i have placed both methods in different services so that spring can achieve nested transaction.注意:这里我把两种方法都放在了不同的服务中,这样spring就可以实现嵌套事务。
Thanks in Advance,提前致谢,
Vali瓦利
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
).当您进入 service2 的 method2 时,service1 的事务(比如
tx1
)被暂停,并且一个新的事务被创建到 service2 (比如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. tx2 将在您从 service2 返回时提交/回滚,之后 tx1 将从暂停的点恢复。 The result of tx2 (whether it resulted in a commit or a rollback) will not affect the behavior of tx1.
tx2 的结果(无论是提交还是回滚)都不会影响 tx1 的行为。
Please read the Spring documentation .请阅读Spring 文档。 Check section 16.5.7 for more information about transaction propagation.
查看第 16.5.7 节以获取有关事务传播的更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.