简体   繁体   English

Spring重试JPA存储库

[英]Spring Retry on JPA repository

I am new to spring, as part of my project implementation, I am supposed to add a spring retry on a service method that invokes JPA repository. 我是Spring的新手,作为项目实现的一部分,我应该在调用JPA存储库的服务方法上添加spring retry。 Code looks something like below 代码如下所示

@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!!!!!!");
}

Database : Postgresql, 数据库 :Postgresql,

Application : Java Spring Boot (Exposed Rest API to get all accounts) 应用程序 :Java Spring Boot(Exposed Rest API以获取所有帐户)

HikariPool MaximumPoolSize ** : **1 HikariPool MaximumPoolSize **:** 1

Hikari ConnectionTimeout ** : **1000 Hikari ConnectionTimeout **:** 1000

JMeter is used to send 10,000 API requests. JMeter用于发送10,000个 API请求。

Problem : I am able to see SQL EXCEPTION CAUGTH!!!!!!!!! 问题 :我能看到SQL EXCEPTION CAUGTH !!!!!!!!! prints but i think retry is not working since I am not seeing the recover method's print. 打印,但我认为重试不起作用,因为我没有看到恢复方法的打印。 am i missing something? 我错过了什么吗?

Below is the gradle dependencies 下面是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'

Try 尝试

@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!!!!!!");
}

Thanks for the support. 感谢您的支持。

I found the issue was that @Recover method had different data type than the @Retryable . 我发现问题是@Recover方法的数据类型与@Retryable Conditions for Retry to work in the above scenario 在上述场景中重试的条件

@Recover and @Retryable should be public and of the same return type @Recover@Retryable应该是公共的并且具有相同的返回类型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM