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