title: Avoid processing same file twice in Spring Batch
body: <p>I have to write a Spring Batch job as follows:</p>
  
  <ul>
  <li>Step 1: Load an XML file from the file system and write its contents to a database staging table</li>
  <li>Step 2: Call Oracle PL/SQL procedure to process the staging table.</li>
  </ul>
  
  <p>(Comments on that job structure are welcome, but not the question).</p>
  
  <p>In Step 1, I want to move the XML file to another directory after I have loaded it.  I want this, as much as possible, to be "transactional" with the write to the staging table.  That is, either both the writes to staging and the file move succeed, or neither does.</p>
  
  <p>I feel this necessary because if (A) the staging writes happen but the file does not move, the next run will pick up the file again and process it again and (B) if the file gets moved but the staging writes do not happen, then we will have missed that file's processing.</p>
  
  <p>This interface's requirements are all about <em>robustness</em>.  I know I could just put a step execution listener to move all the files at the end, but I want the approach that is going to guarantee that we never miss processing data and never process the same file twice.</p>
  
  <p>Part of the difficulty is that I am using a <code>MultiResourceItemReader</code>.  I read that <code>ChunkListener.beforeChunk()</code> happens as part of the chunk transaction, so I tried to make a custom chunk <code>CompletionPolicy</code> to force chunks to complete after each change of resource (file) name, but I could not get it to work.  In any case, I would have needed an <code>afterChunk()</code> listener, which is not part of the transaction anyway.</p>
  
  <p>I'll take any guidance on my specific questions or an expert explanation of how to robustly process files in Spring Batch (which I am only just learning).  Thanks!</p>