简体   繁体   English

在Spring的@Transactional注释的方法中引发并捕获的未经检查的异常是否还会导致事务回滚?

[英]Will an unchecked exception thrown and caught inside a method annotated with spring's @Transactional still cause the transaction to rollback?

I have method in my service class. 我的服务班级中有方法。

@Transactional
public void serviceMethod {
    dao.daoMethod();
}

public void daoMethod() {//dao.daoMethod
    //some code
    try {
        //some more code that throws an unchecked exception
    } catch(Exception exception) {
       //do something -- no exceptions generated/thrown from here
    }
    //some more code
 }

Will this result in the transaction rolling back? 这会导致交易回滚吗? If the Unchecked exception was thrown from within a method that was called from the try block would it be any different? 如果从try块调用的方法中引发了Unchecked异常,它会有所不同吗?

No, the transaction will only be rolled back in case of an uncaught exception. 不可以,只有在发生未捕获的异常的情况下,事务才会回滚。

The transactional interceptors "wrap" around the calls of the annotated methods; 事务拦截器将“包装”在带注释的方法的调用周围。 they cannot see what happens inside them. 他们看不到里面发生了什么。

In your case it will silently ignore the exception. 在您的情况下,它将默默地忽略该异常。 You didn't do anything inside the catch block. 您没有在catch块内执行任何操作。 This is not advised at all. 完全不建议这样做。

If you catch an Exception in the try-catch block and you did some actions to handle this Exception - a rollback will not happen. 如果您在try-catch块中捕获到一个Exception,并且您做了一些操作来处理此Exception-不会发生回滚。 In the case with RuntimeException, by default - the rollback will occur. 对于RuntimeException,默认情况下-将发生回滚。

You can specify, which exceptions should cause the rollback. 您可以指定哪些异常会导致回滚。 @Transactional(rollbackFor = MyCheckedException.class) @Transactional(rollbackFor = MyCheckedException.class)

https://resourcepool.io/2014/11/16/java-quickies-what-you-wish-you-knew-about-spring-transactional-annotation/ https://resourcepool.io/2014/11/16/java-quickies-what-you-wish-you-knew-about-spring-transactional-annotation/

https://www.catalysts.cc/wissenswertes/spring-transactional-rollback-on-checked-exceptions/ https://www.catalysts.cc/wissenswertes/spring-transactional-rollback-on-checked-exceptions/

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

相关问题 回滚 @Transactional 注释的方法 - Rollback a @Transactional annotated method 如果在控制器中引发异常,则Spring JPA回滚事务 - Spring JPA rollback transaction if Exception thrown in controller Spring @Transaction 在抛出异常时不回滚 - Spring @Transaction does not rollback on Exception thrown 抛出异常时如何回滚Spring事务 - How to rollback Spring Transaction when an Exception is thrown 使用@Transactional 注释的方法内部没有捕获异常 - Exception are not getting caught inside method annotate with @Transactional 捕获检查异常时如何使用spring事务回滚更改? - how to use transaction of spring to rollback the changes when checked exception is caught? Spring事务不会在@Transactional方法中的RuntimeException回滚 - Spring Transaction doesn't Rollback at RuntimeException in @Transactional method 测试方法上的 @Transactional 注释是否在结束之前回滚注释方法中的每个事务? - Does @Transactional annotation on test methods rollback every transaction in the annotated method before it gets to the end? 未捕获由lambda抛出的Java未经检查的异常 - Java unchecked exception thrown by lambda is not caught Spring事务在@Transactional方法中处理JMSTemplate - Spring transaction handling JMSTemplate inside @Transactional method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM