Search code examples
springspring-bootjdbcspring-data-jpaconnection-pooling

Spring Retry for Dao Class


I have the below code Spring boot version : 2.3.4

@Repository
class Dbrepository
{


public void performCall() {

 simpleJdbcCall = new 
               SimpleJdbcCall(jdbcTemplate) 
               .withProcedureName("read_actor")
             //.....
  
    simpleJdbcCall.execute(...) ;
}

}

I want to retry the performCall() atleast 3 times at interval of 1 min between each call. How can i achieve this? I am thinking of using Spring Retry ?Is this the best approach?


Solution

  • You can achieve it by adding spring-retry

       <dependency>
            <groupId>org.springframework.retry</groupId>
            <artifactId>spring-retry</artifactId>
        </dependency>
    

    Add @EnableRetry in your main application

    @Retryable(maxAttempts=3,backoff=@Backoff(delay = 10000))
    public void performCall() {
    
     simpleJdbcCall = new 
                   SimpleJdbcCall(jdbcTemplate) 
                   .withProcedureName("read_actor")
                 //.....
      
        simpleJdbcCall.execute(...) ;
    }
    

    Hope useful