Spring Retry with Transactional

前端 未结 4 1600
余生分开走
余生分开走 2021-01-01 13:57

Is Spring Retry guaranteed to work with Spring\'s @Transactional annotation?

Specifically, I\'m trying to use @Retryable for optimistic loc

4条回答
  •  误落风尘
    2021-01-01 14:38

    In case you are using Spring Boot and you want to use @Retryable, this is what you need to do:

    1. Add the dependency to the pom:
    
        org.springframework.retry
        spring-retry
    
    
    1. Enable retries in your Spring Boot application:
    @EnableRetry // <-- Add This
    @SpringBootApplication
    public class SomeApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(SomeApplication.class, args);
        }
    
    }
    
    1. Annotate your method with @Retryable:
    @Retryable(value = CannotAcquireLockException.class,
            backoff = @Backoff(delay = 100, maxDelay = 300))
    @Transactional(isolation = Isolation.SERIALIZABLE)
    public boolean someMethod(String someArg, long otherArg) {
        ...
    }
    

    You can annotate the same method with both @Retryable and @Transactional and it will work as expected.

提交回复
热议问题