简体   繁体   English

如果调用在 Spring 中抛出 RuntimeException 的私有方法,@Transactional 方法是否应该回滚?

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

相关问题 Spring在@Transactional方法中捕获JpaSystemException并回滚事务 - Spring Catch JpaSystemException in @Transactional Method and Roll Back Transaction Spring隐藏由@Transactional方法抛出的RuntimeException - Spring hides RuntimeException thrown by a @Transactional method 控制器方法上的@Transactional 不会回滚事务,但服务方法上会 - @Transactional on Controller Method does not roll back transactions, but on Service Methods does 弹簧靴。 @Transactional 方法调用 @Transactional(readonly=true) 方法 - Spring Boot. @Transactional method calling a @Transactional(readonly=true) method Spring:@Transactional @Scheduled方法抛出TransactionException - Spring : @Transactional @Scheduled method throws TransactionException Spring事务包 - 私有方法 - Spring transactional package-private method Spring @Transactional 属性是否适用于私有方法? - Does Spring @Transactional attribute work on a private method? Spring事务不会在@Transactional方法中的RuntimeException回滚 - Spring Transaction doesn't Rollback at RuntimeException in @Transactional method Java Spring @Transactional方法没有按预期回滚 - Java Spring @Transactional method not rolling back as expected 在调用私有方法的层次结构时,哪种方法应该改变字段? - Which method should change the field, when calling a hierarcy of private methods?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM