[英]Should @Transactional method roll back in case of calling a private method which throws RuntimeException in Spring?
Should transaction rolled back here?交易应该在这里回滚吗?
@Service
public class Serv {
@Transactional
public void method1() {
method2();
}
private void method2(){
throw new RuntimeException();
}
}
Or does it depend on something?或者它取决于什么?
( Before today I was 100% sure in the described behavior. But an interviewer said it depends. I disagreed with him but of course it doesn't help.) (在今天之前,我对所描述的行为有 100% 的把握。但是面试官说这要视情况而定。我不同意他的看法,但这当然没有帮助。)
The optimal/usual result is rollback as you answered, but interviewer said general statement as it depends最佳/通常的结果是你回答的回滚,但面试官说的是一般性陈述,这取决于
Maybe the interviewer meant to check if you know when it won't rollback as expected也许面试官的意思是检查你是否知道它什么时候不会按预期回滚
Meaning that you shouldn't assume @Transactional
is actually working, it should be checked first, because if @Transactional
isn't working there'll be no rollback.这意味着您不应该假设@Transactional
实际上在工作,应该首先检查它,因为如果@Transactional
不工作,则不会回滚。
For example, in case of calling transactional method in same class , if method1() is called from method3() in same class @Transactional
won't work and won't rollback例如,如果在同一个类中调用事务方法,如果从同一个类中的 method3() 调用 method1() @Transactional
将不起作用并且不会回滚
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.