Suman
Suman

Reputation: 173

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. 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,

Application : Java Spring Boot (Exposed Rest API to get all accounts)

HikariPool MaximumPoolSize ** : **1

Hikari ConnectionTimeout ** : **1000

JMeter is used to send 10,000 API requests.

Problem : I am able to see 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

// 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'

Upvotes: 5

Views: 8487

Answers (2)

Suman
Suman

Reputation: 173

Thanks for the support.

I found the issue was that @Recover method had different data type than the @Retryable. Conditions for Retry to work in the above scenario

@Recover and @Retryable should be public and of the same return type

Upvotes: 5

DCO
DCO

Reputation: 1292

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

Upvotes: 0

Related Questions