【问题标题】:@Retryable with Spring @Transactional@Retryable 与 Spring @Transactional
【发布时间】:2021-12-30 08:35:58
【问题描述】:

我有一个项目需要在事务方法上使用 retriable。假设我想更新一些客户信息,我需要重试更新 2 次,直到。

我把我的 pom 依赖放在了

       <dependency>
            <groupId>org.springframework.retry</groupId>
            <artifactId>spring-retry</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

在我的@Configuration 类中,我有@EnableRetry。 在我的@Service 类中,我定义了一个方法:

>  @Retryable(value = CannotAcquireLockException.class, maxAttempts = 3,
> backoff = @Backoff(delay = 1000,multiplier = 2,maxDelay = 5000))
>     @Transactional
>     public void updateClient (String info) throws Exception {
>         updateClientFromDB(info);
>     }

我从 Junit5 测试我的方法,似乎 @Retryable 不起作用。我在 db 中锁定了一行,当我运行测试时,它似乎没有重试,它无限循环。

> @ExtendWith(SpringExtension.class) 
> @ContextConfiguration(classes = {DSConfig.class }) 
> @ActiveProfiles({ "test" }) 
> @Slf4j class Test {
> 
> @Test     
> void retryTest() {        
>  String info = "Test";        
>  updateClient(info);  } }

【问题讨论】:

  • 所以你说的是 Retryable 正在工作,因为代码是“循环的”,真正的问题是它没有像你预期的那样停止 3 次尝试。正确的?正如其他人所问的那样,您是否收到任何异常或控制台消息?

标签: spring transactions spring-retry


【解决方案1】:

updateClientFromDB 会抛出CannotAcquireLockException 吗?

【讨论】:

    猜你喜欢
    • 2018-02-14
    • 1970-01-01
    • 1970-01-01
    • 2015-01-15
    • 2015-02-23
    • 1970-01-01
    • 2014-06-30
    • 2018-06-30
    • 2018-08-08
    相关资源
    最近更新 更多