Code: Select all
@EnableRetry
@EnableTransactionManagement
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
< /code>
@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
}
}
< /code>
public class MyRetryListener implements RetryListener {
@Override
public boolean open(RetryContext context, RetryCallback callback) {
log.info("Retry open.");
return true;
}
@Override
public void close(RetryContext context, RetryCallback 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 void onError(RetryContext context, RetryCallback callback, Throwable throwable) {
log.warn("Inner retry {} of {} failed due to: {}", context.getRetryCount(), context.getAttribute("context.max-attempts"), throwable.getMessage());
}
}
Hat jemand etwas Ähnliches erlebt? Lang-Java PrettyPrint-Override ">
Code: Select all
@SpringBootTest
@ActiveProfiles("test")
@EnableAutoConfiguration
@AutoConfigureMockMvc
class MyApplicationIntegrationTest {
}
< /code>
Die Anwendung schreibt einige Datensätze in eine MS SQL Server -Datenbank, die ich im Test mit einem H2 in der Speicherdatenbank simuliert habe. Im Integrationstest werfe ich eine SQLEXception über einen "Before Insert 'Trigger in der H2 -Tabelle2025-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.
< /code>
Während die Ausgabe des Tests leider: < /p>
ist2025: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.