简体   繁体   English

链式交易注释的奇怪行为

[英]Strange behaviour for chainned transactional annotation

I don't get how it works the transactional annotations of Spring. 我不知道它是如何工作的Spring的事务注释。 So I made the next test with no practical sense but I It shows my problem: 因此,我没有实际意义进行了下一次测试,但我发现了我的问题:

public class TransactionalTest {
    public void noTransaction(){
        required();
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    public void supports(){
        required();
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void transaction(){
        required();
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void required(){
        mandatory();
    }

    @Transactional(isolation = Isolation.READ_UNCOMMITTED, propagation = Propagation.MANDATORY)
    public void mandatory(){
        doSomething();
    }

    private void doSomething(){
        //I don't feel like to do something.
    }
}

Methods noTransaction , supports and transaction call to the same method: required but only the last one ( transaction ) works properly. 方法noTransactionsupports和对同一方法的transaction调用: required但只有最后一个( transaction )可以正常工作。 The two others give me back the message No existing transaction found for transaction marked with propagation 'mandatory' . 另外两个还给我以下消息:找不到针对带有传播“强制性”标记的交易的现有交易

In the real case, I have some non transactional methods which calls to transactional methods annotated with REQUIRED (It works fine). 在实际情况下,我有一些非事务方法,这些方法调用以REQUIRED注释的事务方法(工作正常)。 but if a non transactional method calls to a transactional method (annotated with REQUIRED ) which it calls to another transactional method annoted with MANDATORY , it fails. 但是,如果非事务方法调用一个事务方法(用REQUIRED注释),而该事务方法又调用了另一个用MANDATORY注释的事务方法,则它将失败。

Why this behaviour and how can avoid it? 为什么这种行为以及如何避免呢? I annotated annotate all the method which calls to a transaccional method just in case? 我注释了所有调用transaccional方法的方法,以防万一?

If you're using the default AOP method, Spring Proxy AOP, the advice is implemented by wrapping the annotated class with a proxy object that intercepts and advises the calls. 如果您使用默认的AOP方法Spring Proxy AOP,则通过将带注释的类与代理对象一起包装来实现建议,该代理对象可以拦截并建议调用。 When you use self-calls like this, you're bypassing the proxy, and the advice doesn't get applied. 当您使用这样的自我呼叫时,您将绕过代理服务器,并且该建议不会被应用。

If you really do need to have advice applied on self-calls, you need to use AspectJ AOP, which actually modifies the class in question instead of decorating it. 如果确实需要在自调用上应用建议,则需要使用AspectJ AOP,它实际上是在修改所讨论的类而不是修饰它。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM