title: Cannot catch exception in spring batch's ItemWriter
body: <p>I'm writing a Spring Batch process to migrate a dataset from one system into another. In this case this is as simple as using a <code>RowMapper</code> implementation to build the object from a query before handing off to the <code>ItemWriter</code>. The <code>ItemWriter</code> calls the <code>save</code> method on my DAO (defined as an interface and handled by spring data)</p>
  
  <p>The issue is this: I have a unique constraint on the MyItem table, and therefore saving a duplicated record will result in a <code>DataIntegrityViolationException</code>. I have tried catching this within the <code>ItemWriter</code> to allow me to log that a record was not imported, however during execution it never enters this catch statement. I have attempted catching <code>Exception</code> AND <code>Throwable</code> to no avail too.</p>
  
  <p>From what I've noticed, there is a <code>@Transactional</code> annotation on the 'save' method of my DAO where I would expect the commit and flush to occur. Does Spring Batch alter this transaction in any way? Such that the <code>@Transactional</code> annotation applies to the 'write' method of the <code>ItemWriter</code>?</p>
  
  <p>Can I even catch the exception in this class?</p>
  
  <p>I've provided code snippets below, if you need further information - please let me know.</p>
  
  <p>Many thanks for any help you can provide</p>
  
  <p><strong>ItemWriter</strong></p>
  
  <pre><code>@Component
  public class MyItemWriter implements ItemWriter&lt;MyItem&gt; {
  
      private static final Logger LOG = LoggerFactory.getLogger(MyItemWriter.class);
  
      @Resource
      private MyItemDao myItemDao;
  
      @Override
      public void write(List&lt;? extends MyItem&gt; myItems) throws Exception {
          for (MyItem myItem : myItems) {
              try {
                  myItemDao.save(myItem);
              } catch (Throwable ex) {
                  LOG.warn("Failed to import MyItem: {}: {} ", myItem.getId(), ex.toString());
              }
          }
      }
  }
  </code></pre>
  
  <p><strong>DAO</strong></p>
  
  <pre><code>public interface MyItemDao extends PagingAndSortingRepository&lt;MyItem, Integer&gt; {
      [Custom methods omitted]
  }
  </code></pre>
  
  <p><strong>Spring Batch configuration</strong></p>
  
  <pre><code>&lt;batch:job id="myImportJob" restartable="true" job-repository="jobRepository"&gt;
      &lt;batch:step id="myImportStep" allow-start-if-complete="true"&gt;
          &lt;batch:tasklet&gt;
              &lt;batch:chunk reader="myItemReader" writer="myItemWriter" commit-interval="50" /&gt;
          &lt;/batch:tasklet&gt;
      &lt;/batch:step&gt;
  &lt;/batch:job&gt;
  </code></pre>