简体   繁体   中英

@Transactional(isolation = Isolation.SERIALIZABLE) retry mechanism

 @Transactional(isolation = Isolation.SERIALIZABLE)

I have that annotation on several methods in my spring project. If there is an exception due to the "serialize access problems" what is the best approach if I want to retry the specific transaction. There is annotation @Retryable but it is not very straightforward to me how to use it so that the transaction will rollback and then retry only for that specific exception and just rollback for the other runtime exceptions. Thanks in advance.

A simple solution is to have a method that is the "entry point" for performing your logic; which delegates the actual logic to a transactional method. Typically a nice way of doing this is to have one class that has the Transactional annotations and that does the work and another which is the interface for clients to interact with that delegates; providing a form of indirection.

private static final int MAX_RETRY = 5;
public void doWork(T... parameters) {
    doWork(0, parameters);
}

private void doWork(int retryLevel, T... parameters) {
    if (retryLevel == MAX_RETRY) {
        throw new MaximumRetryCountException(); //or any other exception
    } else {
        try {
           //Get your Spring context through whatever method you usually use
           AppContext().getInstance().getBean(classInterestedIn.class).doTransactionalMethod(parameters);
        } catch (ExceptionToRetryFor e) {
            doWork((retryLevel + 1), parameters);
        }
    }
}

@Transactional(isolation = Isolation.SERIALIZABLE)
public void doTransactionalMethod(parameters) {
    ...
}

Please note you may run into problems calling a Transactional method from a different method within that same class (ie calling this.doTransactionalMethod()) hence the invocation of Transactional Method is through the Spring Application Context. This is due to the way Spring AOP wraps classes to engage transactional semantics. See: Spring @Transaction method call by the method within the same class, does not work?

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