Sum*_*man 5 java jpa spring-retry spring-boot
我是 spring 的新手,作为项目实现的一部分,我应该在调用 JPA 存储库的服务方法上添加 spring 重试。代码如下所示
@Retryable(value = {Exception.class},maxAttempts = 1,backoff = @Backoff(300))
public Page<Account> findAllAccounts(AccountSearchRequest account, Pageable pageable) {
try {
return map(accountSearchRepository.findAll(account, pageable));
}catch (Exception e){
System.out.println("SQL EXCEPTION CAUGTH!!!!!!!!!");
}
return null;
}
@Recover
public void recover(Exception e){
System.out.println("!!!!!!!!!!!Failed to get connection!!!!!!");
}
Run Code Online (Sandbox Code Playgroud)
数据库:Postgresql,
应用程序:Java Spring Boot(公开 Rest API 以获取所有帐户)
HikariPool 最大池大小 ** : **1
Hikari 连接超时 ** : **1000
JMeter 用于发送10,000 个API 请求。
问题:我能够看到SQL EXCEPTION CAUGTH!!!!!!!!! 打印,但我认为重试不起作用,因为我没有看到恢复方法的打印。我错过了什么吗?
下面是gradle依赖项
// https://mvnrepository.com/artifact/org.springframework.retry/spring-retry
compile group: 'org.springframework.retry', name: 'spring-retry', version: '1.2.4.RELEASE'
// https://mvnrepository.com/artifact/org.springframework/spring-aspects
compile group: 'org.springframework', name: 'spring-aspects', version: '3.2.4.RELEASE'
// https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-aop
compile group: 'org.springframework.boot', name: 'spring-boot-starter-aop', version: '2.1.3.RELEASE'
// https://mvnrepository.com/artifact/org.springframework/spring-aop
compile group: 'org.springframework', name: 'spring-aop', version: '5.1.5.RELEASE'
Run Code Online (Sandbox Code Playgroud)
感谢您的支持。
我发现问题是该@Recover方法的数据类型与@Retryable. 上述场景中Retry工作的条件
@Recover并且@Retryable应该是公共的并且具有相同的返回类型
尝试
@Retryable(value = {SQLException.class},maxAttempts = 1,backoff = @Backoff(300))
public Page<Account> findAllAccounts(AccountSearchRequest account, Pageable pageable) throws SQLException {
throw new SQLException("test");
}
@Recover
public void recover(Exception e){
System.out.println("!!!!!!!!!!!Failed to get connection!!!!!!");
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7487 次 |
| 最近记录: |