简体   繁体   English

Spring 事务:从带有 requires 的方法调用的带有 requires_new 的方法

[英]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.

相关问题 Spring交易REQUIRES_NEW是否会传播到方法中的方法? - Will Spring transaction REQUIRES_NEW be propagated to the methods within the method? 代理的REQUIRES_NEW方法不会回滚(春季) - Proxied REQUIRES_NEW method doesn't rollback (spring) EJB 调用的 Spring 事务:当 Try 的事务方法出现 RuntimeException 时,Catch 中的事务方法需要 REQUIRES_NEW - Spring transaction called by EJB: REQUIRES_NEW needed for transactional method in Catch, when Try's transactional method has RuntimeException Spring事务中REQUIRES_NEW和NESTED传播行为的差异 - Differences in behaviour of REQUIRES_NEW and NESTED propagation in Spring transactions Spring 事务中 requires_new 和嵌套传播的区别 - Differences between requires_new and nested propagation in Spring transactions Spring数据gemfire不支持REQUIRES_NEW个事务 - REQUIRES_NEW transactions in not supported by Spring data gemfire Spring 事务从 NOT_SUPPORTED 到 REQUIRES_NEW 的传播 - Spring Transaction Propagation from NOT_SUPPORTED to REQUIRES_NEW 循环传播新事务(使用REQUIRES_NEW)不能按预期方式进行 - Propagation of new transactions (using REQUIRES_NEW) in a loop is not working as expected EJB Transactions属性:NOT_SUPPORTED和REQUIRES_NEW - EJB Transactions attribute: NOT_SUPPORTED and REQUIRES_NEW REQUIRES_NEW在Spring + Hibernate中不创建新交易 - REQUIRES_NEW not creating a new transaction in spring+hibernate
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM