Can Spring Boot @Retryable work with the Spring Data JPA save() method? My attempt retries once and then fakes success

I am trying to implement retries on the methods of a class that extends JpaRespository. For all the methods annotated with @Query the retries work as expected (3 retries, and then the error is thrown if unsuccessful). However, when the inherited save() function is called and a database exception is thrown, only one retry is performed, and then program execution continues as if the query were successful (even though it wasn’t).

The MSSQL database connection is configured with Spring Boot. The repository is defined similarly to the below:

@EnableJpaRepositories
@Repository
@Retryable(listeners = {"retryListeners"})
public interface ExampleRepo extends JpaRepository<ExampleRecord, Long>{

@Query("select * from ExampleRepo where exampleParam = :exampleParam")
List<ExampleRecord> exampleQuery(@Param("exampleParam") String exampleParam);

}

The save method is being called on an instance of the repo from another class.

exampleRepo.save(exampleRecord);

Editing to add that my AppConfig class is like below:

@Slf4j
@Configuration
@EnableRetry
public class AppConfig {

// unrelated configurations

// Retry listener bean implemented similarly to this response https://stackoverflow.com/a/50704746/22562155

}

The output I get in the console comes in this order:

  1. “Starting retryable method” from the retryListener
  2. Hibernate debug logs with the query and parameter binding information
  3. SqlExceptionHelper logs with error info, eg that data would be truncated
  4. “Retryable method threw exception” from the retryListener
  5. “Finished retryable method” from the retryListener
  6. Logs from continued normal code execution; no exception is thrown

I have stepped through the execution with the IntelliJ debugger, and I can tell that somehow the retry implementation concludes that the retryable method succeeded, but I’m having trouble understanding why. I can’t find any answers online about this issue to reference; I am hoping someone else knows whether the save() method is incompatible with the Retryable annotation, or if there is something I need to fix for this to work.

  • do you have @EnableRetry in your AppConfig class ?

    – 

  • @Ashish Yes, I do

    – 

Leave a Comment