简体   繁体   English

外部交易和内部交易之间有什么关系?

[英]What is the relationship between outer transaction and inner transaction?

I am new to Hibernate and when I am using transactions I met couple of questions. 我是Hibernate的新手,在使用事务处理时遇到了几个问题。 Previously, the situation is 以前的情况是

@Transactional
Class A {
  methodA1(){
    DOs = fetchDOsFromDB();
    methodA2(DOs);
  }
    methodA2(DOs){
    ClassB.methodB1(DOs);
  }
}

Class B{
  @Transactional(propagation = Propagation.REQUIRES_NEW)
  methodB1(DOs){
    dealWithDOs();
  }
}

But I found when the amount of DOs is huge, it will give connection is closed exception. 但是我发现当DO的数量巨大时,它将给连接被关闭的异常。 So I am wondering if the root cause is I passed the DOs to methodB1 which bound with transaction in A and the transaction A can't be committed for a long time. 所以我想知道根本原因是否是我将DO传递给了方法B1,该方法与A中的事务绑定,并且事务A不能长时间提交。

So this is the first question, I know @Transactional(propagation = Propagation.REQUIRES_NEW) will make the outer transaction be suspended but what does suspend exactly mean? 所以这是第一个问题,我知道@Transactional(propagation = Propagation.REQUIRES_NEW)将使外部事务被暂停,但是暂停的确切含义是什么? Does it have timeout? 有超时吗?

And if I don't pass DOs to classB , instead, I do 如果我不将DOs传递给classB ,我会

methodA1(){
DOs = fetchDOsFromDB();
List<Integer> list = getIds(DOs);
methodA2(list);
}

methodA2(list){
 ClassB.methodB1(list);
}

which means I am not relying on DOs , will this help? 这意味着我不依赖DOs ,这会有所帮助吗?

Is there any relationship between the commit of transaction A and the running result of transaction B? 事务A的提交与事务B的运行结果之间是否有任何关系? In other words, does the commit of transaction A need to wait for the completion of methodB1 ? 换句话说,事务A的提交是否需要等待methodB1的完成?

When you use Propagation.REQUIRES_NEW you are telling the persistence provider to start what is called a nested transaction . 使用Propagation.REQUIRES_NEW您要告诉持久性提供程序启动所谓的嵌套事务 Oracle documentation has some good explanation of this topic: Oracle文档对此主题有一些很好的解释:

  1. What does suspend exactly mean? 暂停到底是什么意思?

    While the nested (child) transaction is active, the parent transaction may not perform any operations other than to commit or abort, or to create more child transactions. 当嵌套(子)事务处于活动状态时,父事务可能不执行任何其他操作(提交或中止或创建更多子事务)。

  2. Is there any relationship between the commitment of transaction A and the running result of transaction B? 事务A的承诺和事务B的运行结果之间是否有任何关系?

    Committing a nested transaction has no effect on the state of the parent transaction. 提交嵌套事务不会影响父事务的状态。 The parent transaction is still uncommitted. 父事务仍未提交。 However, the parent transaction can now see any modifications made by the child transaction. 但是,父事务现在可以看到子事务所做的任何修改。 Those modifications, of course, are still hidden to all other transactions until the parent also commits. 当然,这些修改对所有其他事务仍然是隐藏的,直到父级也提交为止。

  3. In other words, does the commitment of transaction A need to wait for the completion of methodB1? 换句话说,事务A的承诺是否需要等待方法B1的完成?

    If the parent transaction commits or aborts while it has active children, the child transactions are resolved in the same way as the parent. 如果父事务在有活动子进程的情况下提交或中止,则子事务与父事务的解析方式相同。 That is, if the parent aborts, then the child transactions abort as well. 也就是说,如果父项中止,那么子事务也将中止。 If the parent commits, then whatever modifications have been performed by the child transactions are also committed. 如果父项提交,则子事务已执行的任何修改也将提交。

So, no A does not need to wait for B to commit, which can actually rollback and A can still commit. 因此,没有A不需要等待B提交,这实际上可以回滚并且A仍然可以提交。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM