简体   繁体   中英

Strange behaviour for chainned transactional annotation

I don't get how it works the transactional annotations of 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. 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). 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.

Why this behaviour and how can avoid it? I annotated annotate all the method which calls to a transaccional method just in case?

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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