title: Spring retry template is not working with @org.springframework.transaction.annotation.Transactional
body: <p>In the case of database unavailability, I need to retry executing my service method 3 times. For this, I use Spring Retry Template. But with @Transactional annotation, when DB is unavailable a specific exception can't be caught. </p>
  
  <pre><code>@Configuration
  public class RetryTemplateConfig {
  
      @Bean
      public  RetryTemplate createRetryTemplate(){
          RetryTemplate retryTemplate = new RetryTemplate();
  
          FixedBackOffPolicy fixedBackOffPolicy = new FixedBackOffPolicy();
          fixedBackOffPolicy.setBackOffPeriod(5000l);
          retryTemplate.setBackOffPolicy(fixedBackOffPolicy);
  
          SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
          retryPolicy.setMaxAttempts(5);
          retryTemplate.setRetryPolicy(retryPolicy);
  
          return retryTemplate;
      }
  
  }
  
  
  @Service
  public class MessageService {
  
      @Autowired
      private RetryTemplate retryTemplate;
  
      @Autowired
      MessageRepo messageRepo;
  
      @Transactional
      public void sendMessage(Message message) throws RuntimeException{
  
          retryTemplate.execute(retryContext -&gt; {
              log.info("Executing for {}  " ,  retryContext.getRetryCount());
              Message model = new Message();
              messageRepo.save(model);
              return "";
          });
      }
  }
  </code></pre>
  
  <p>But if I try using @Retryable when the database is unavailable the retry happens successfully for RuntimeExcption and calls the @Recover method at the end of the retry.</p>
  
  <pre><code>@Service
  @EnableRetry
  public class MessageService {
  
      @Autowired
      private RetryTemplate retryTemplate;
  
      @Autowired
      MessageRepo messageRepo;
  
      @Retryable(
              value = {RuntimeException.class},
              backoff = @Backoff(delay = 2000),
              maxAttempts = 5
      )
      @Transactional
      public void sendMessage(Message message) throws RuntimeException{
          Message model = new Message();
          messageRepo.save(model);
      }
  }
  
  
      @Recover
      public void recover(){
          log.info("Recover is called ");
      }
  }
  </code></pre>
  
  <p>Can someone please explain the reason for these two different behaviours and suggest me the best way of retrying? (I'm using JPA data)</p>