title: Catch an SQL Exception with Spring Data JPA
body: <p>The following methods tries to write data to DB (<code>firmRepository.save()</code>) and to ElasticSearch (<code>firmSearchRepository.save()</code>).</p>
  
  <p>The problem is that I do not want to write to ES repository if the DB save failed. I am trying to catch the exception with <code>DataAccessException</code>, but through my tests I see that such exceptions as unique constraint violation -> get through and persistence in ElasticSearch still executes. </p>
  
  <pre><code>@Override
  public FirmDTO save (FirmDTO firmDTO) {
      log.info("Database Request to save Firm: {}", firmDTO.getFirmId());
      Firm firm = firmMapper.firmDTOtoFirm(firmDTO);
      try {
          firm = firmRepository.save(firm);
          firmSearchRepository.save(firm);
          return firmMapper.firmToFirmDTO(firm);
      } catch (DataAccessException ex) {
          log.error(ex.getLocalizedMessage());
          return null;
      }
  }
  </code></pre>
  
  <p>Is there way to catch those specific exceptions related to SQL?</p>