Java – spring @ transactional annotation property priority / inheritance

When the calling method itself is transactional, when required propagates, if they are different, will the current method overwrite closed transaction attributes (such as rollback for)?

Illustration:

Class A {
    @Transactional(propagation = Propagation.required,rollbackFor = { SomeException.class})
    void foo() {
        try {
           b.bar();
        } catch (OtherException e) {
           // is the transaction marked as rollback-only at this point ?
        }
    }
}

Class B {
    @Transactional(propagation = Propagation.required,rollbackFor = { OtherException.class})
    void bar() {
        [...]
    }
}

Edit:

Well, I want to avoid trivial out of range answers, so let's be clear, I know spring propagation processing

If you are not, here is the relevant part of the document. I just want to clarify the first part of the above example:

My question can be rewritten as:

Does the logical transaction scope contain transaction attributes?

Solution

Therefore, I set up a test case, and the short answer is yes

The transaction logic scope contains transaction attributes, and its boundary is indeed annotated method attributes

Therefore, even if the underlying physical transactions of the two methods are the same, the logical attributes apply to each method, and the internal method can force the rollback of the external method transaction If the commit is triggered for the last time, it will result in unexpected rollbackexception

Compare with spring transactioninterceptor (comments are mine)

try {
        retVal = invocation.proceed();
}
catch (Throwable ex) {
        completeTransactionAfterThrowing(txInfo,ex);
        throw ex;
}

completeTransactionAfterThrowing():

// txinfo is proper to the invocation target method
if (txInfo.transactionAttribute.rollbackOn(ex)) {
            try {
                txInfo.getTransactionManager().rollback(txInfo.getTransactionStatus());
            }

AbstractPlatformTransactionManager. processRollback():

else if (status.isNewTransaction()) { //requiresnew
    doRollback(status);
}
else if (status.hasTransaction()) { //requiered
        [...]
        doSetRollbackOnly(status);
    }
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>