JCalcines
JCalcines

Reputation: 1286

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?

Upvotes: 1

Views: 160

Answers (1)

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.

Upvotes: 1

Related Questions