简体   繁体   English

Spring内部事务中的UnexpectedRollBackException

[英]UnexpectedRollBackException in Spring inner transaction

I have two classes: 我有两节课:

@Service
@Transaction
class A {
    public void method1() {
        private B;

        try {
            save1()
            b.method2()
        } catch (SqlException e) {
            doSomeThing();
        }

       @Autowired
       public setB(){
         this.B = B;
       }
    }
}

@Service
class B {

    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    public void method2(){
        save2()
        throw new SqlException();
    }

}

I got an SqlException as expected, but also an UnexpectedRollBackException , and the program stops. 我按预期得到了一个SqlException ,但也是一个UnexpectedRollBackException ,程序停止了。 I want to know why the data persisted by save2() is not rolled back? 我想知道为什么save2()持久保存的数据没有回滚?

Is it a problem with outer transaction? 这是外部交易的问题吗?

UPDATE: I tried catching UnexpectedRollBackException in class A and everything works fine. 更新:我尝试在A类中捕获UnexpectedRollBackException ,一切正常。 But I still need some kind of explanation why I get the exception? 但我仍然需要某种解释为什么我得到例外? I suppose the outer transaction should be suspended when the inner transaction begins, so why the rollback is unexpected for the outer transaction? 我想在内部事务开始时应该暂停外部事务,那么为什么外部事务的回滚是意外的呢?

Thanks. 谢谢。

First of all: you are not injecting the instance of B into class A via spring. 首先:你不是通过弹簧将B的实例注入A类。 ie your b is not managed by spring => this leads to the behaviour: Spring is ignoring the @Transactional annotation on method. 即你的b不是由spring =>管理的,这会导致行为:Spring忽略了方法上的@Transactional注释。

Second if you don't want to rollback on SqlException you have to specify noRollbackFor=SqlException.class 其次,如果你不想回滚上SqlException你必须指定noRollbackFor=SqlException.class

UPDATE: after clarification, the call is happening on managed bean. 更新:澄清后,调用发生在托管bean上。 But the problem that expected behaviour - transaction inside of transaction is not supported in general by the transaction management system out of the box. 但是,预期行为的问题 - 交易内部的交易通常不受交易管理系统的支持。 https://docs.spring.io/spring/docs/4.3.11.RELEASE/javadoc-api/org/springframework/transaction/annotation/Propagation.html#REQUIRES_NEW https://docs.spring.io/spring/docs/4.3.11.RELEASE/javadoc-api/org/springframework/transaction/annotation/Propagation.html#REQUIRES_NEW

Unless the details on that system are provided it is impossible to step forward. 除非提供有关该系统的详细信息,否则无法前进。 Out of content the NESTED transaction propagation could be better, then REQUIRES_NEW, because it is making a rollback point for the external transaction and starts new one. 在内容之外,NESTED事务传播可能会更好,然后REQUIRES_NEW,因为它正在为外部事务创建回滚点并启动新事务。

I have a simliar scenario like this and resolved it using propagation = Propagation.NESTED from where the second method is calling. 我有一个像这样的simliar场景,并使用propagation = Propagation.NESTED从第二个method调用的地方解决它。 rewrite the code as mentioned below and try. 重写下面提到的代码并尝试。

@Service
class A {
@Transactional(rollbackFor = { HibernateException.class}, propagation = Propagation.NESTED)
    public void method1() {
        private B;

        try {
            save1()
            b.method2()
        } catch (SqlException e) {
            doSomeThing();
        }

       @Autowired
       public setB(){
         this.B = B;
       }
    }
}

Note: Transactional is used in method level for class A with propagation nested . 注意: Transactional用于class A方法级别,并且propagation nestedpropagation nested Class B shown below. Class B如下所示。

@Service
class B {

   @Transactional(rollbackFor = {Exception.class}, propagation = Propagation.REQUIRED)
    public void method2(){
        save2()
        throw new SqlException();
    }

}

This has resolved the issue in my case. 这解决了我的问题。

暂无
暂无

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

相关问题 带有 AOP @AfterReturning 的 Spring 中出现 UnexpectedRollbackException - UnexpectedRollbackException in Spring with AOP @AfterReturning 嵌套的@Transactional方法中的Spring UnexpectedRollbackException - Spring UnexpectedRollbackException in nested @Transactional method 批处理异常UnexpectedRollbackException-事务回滚 - Exception on batch processing UnexpectedRollbackException - Transaction rolled back spring引导内事务方法中处理异常 - Handle exception in spring boot inner transaction method 如果内部事务失败但内部事务应该保存数据,如何回滚外部事务 - How to rollback outer transaction in case of failed inner transaction in but inner transaction should save data Spring org.springframework.transaction.UnexpectedRollbackException:事务回滚,因为已将其标记为仅回滚 - org.springframework.transaction.UnexpectedRollbackException: Transaction rolled back because it has been marked as rollback-only UnexpectedRollbackException:事务回滚,因为它已被标记为仅回滚,同时 dontRollbackOn 已设置 - UnexpectedRollbackException: Transaction rolled back because it has been marked as rollback-only meanwhile the dontRollbackOn was already set UnexpectedRollbackException:事务回滚,因为它已被标记为仅回滚 - UnexpectedRollbackException: Transaction rolled back because it has been marked as rollback-only 事务回滚,因为它已被标记为仅回滚 - UnexpectedRollbackException - Transaction rolled back because it has been marked as rollback-only - UnexpectedRollbackException 春天@交易 - Spring @Transaction
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM