Spring Boot集成测试中@Retryable仅重试一次问题求助
Spring Boot @Retryable集成测试仅重试一次问题
我在Spring Boot 2.7.18(JDK 21)应用中使用@Retryable注解,功能运行完全正常!以下是相关代码片段:
启动类代码
@EnableRetry @EnableTransactionManagement @SpringBootApplication public class MyApplication { public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } }
服务类代码
@Service public class MyService { @Retryable( retryFor = {LockAcquisitionException.class, SQLServerException.class}, maxAttempts = 5, backoff = @Backoff(delay = 500), listeners = "myRetryListener" ) @Transactional @Override public void doSomething() throws MyException { // Does something } }
重试监听器代码
public class MyRetryListener implements RetryListener { @Override public <T, E extends Throwable> boolean open(RetryContext context, RetryCallback<T, E> callback) { log.info("Retry open."); return true; } @Override public <T, E extends Throwable> void close(RetryContext context, RetryCallback<T, E> callback, Throwable throwable) { int attempt = context.getRetryCount(); if (throwable == null) { log.info("Operation succeeded after {} attempts.", attempt); } else { log.info("Operation failed after {} attempts.", attempt); } } @Override public <T, E extends Throwable> void onError(RetryContext context, RetryCallback<T, E> callback, Throwable throwable) { log.warn("Inner retry {} of {} failed due to: {}", context.getRetryCount(), context.getAttribute("context.max-attempts"), throwable.getMessage()); } }
但在执行集成测试时,重试机制虽生效却仅重试一次,怀疑测试中缺少某些初始化配置,但无法定位问题所在。
集成测试类代码
@SpringBootTest @ActiveProfiles("test") @EnableAutoConfiguration @AutoConfigureMockMvc class MyApplicationIntegrationTest { }
应用会向MS SQL Server写入数据,测试中使用H2内存数据库进行模拟,通过H2表的BEFORE INSERT触发器抛出SQLException。
应用端日志(符合预期)
2025-01-29 12:05:14 INFO .MyRetryListener : Retry open. 2025-01-29 12:05:15 WARN SqlExceptionHelper : SQL Error: 50000, SQLState: S0001 2025-01-29 12:05:15 ERROR SqlExceptionHelper : Simulated SQLException for testing 2025-01-29 12:05:15 WARN .MyRetryListener : Inner retry 1 of 5 failed due to: Could not persist entities 2025-01-29 12:05:15 WARN SqlExceptionHelper : SQL Error: 50000, SQLState: S0001 2025-01-29 12:05:15 ERROR SqlExceptionHelper : Simulated SQLException for testing 2025-01-29 12:05:15 WARN .MyRetryListener : Inner retry 2 of 5 failed due to: Could not persist entities 2025-01-29 12:05:16 WARN SqlExceptionHelper : SQL Error: 50000, SQLState: S0001 2025-01-29 12:05:16 ERROR SqlExceptionHelper : Simulated SQLException for testing 2025-01-29 12:05:16 WARN .MyRetryListener : Inner retry 3 of 5 failed due to: Could not persist entities 2025-01-29 12:05:16 WARN SqlExceptionHelper : SQL Error: 50000, SQLState: S0001 2025-01-29 12:05:16 ERROR SqlExceptionHelper : Simulated SQLException for testing 2025-01-29 12:05:16 WARN .MyRetryListener : Inner retry 4 of 5 failed due to: Could not persist entities 2025-01-29 12:05:17 WARN SqlExceptionHelper : SQL Error: 50000, SQLState: S0001 2025-01-29 12:05:17 ERROR SqlExceptionHelper : Simulated SQLException for testing 2025-01-29 12:05:17 WARN .MyRetryListener : Inner retry 5 of 5 failed due to: Could not persist entities 2025-01-29 12:05:17 INFO .MyRetryListener : Operation failed after 5 attempts.
测试端日志(仅重试一次)
2025:01:29 12:12:37 INFO .MyRetryListener : Retry open. 2025:01:29 12:12:37 WARN TriggerImpl : Executing database trigger H2TriggerImpl: Simulated SQLException for testing 2025:01:29 12:12:37 WARN SqlExceptionHelper : SQL Error: 0, SQLState: null 2025:01:29 12:12:37 ERROR SqlExceptionHelper : Simulated SQLException for testing 2025:01:29 12:12:37 INFO .AbstractBatchImpl : HHH000010: On release of batch it still contained JDBC statements 2025:01:29 12:12:38 WARN .MyRetryListener : Inner retry 1 of 5 failed due to: Could not persist entities 2025:01:29 12:12:38 INFO .MyRetryListener : Operation failed after 1 attempts.
内容的提问来源于stack exchange,提问作者Gep
相关产品推荐
相关产品推荐

