title: Spring Batch Exception in Chunk
body: <p>I have a spring batch job which has multiple steps.</p>
  
  <p>Step 1 : Loads 10 records from the database. (Tasklet does that job)</p>
  
  <p>Step 2: Chunk Oriented processing in configured here using ItemReader,ItemProcessor,ItemWriter implementations with commit -interval =1 </p>
  
  <p>Now as I understand , for every record this would happen</p>
  
  <p>Begin Transaction ( Read - Process - Write ) Commit Tx</p>
  
  <ol>
  <li><p>My problem is imagine it processed six records and now with the 7th Record it got an exception in the ItemProcessor Implementation, It tries to rollback but not able to rollback due to transaction in unknown state </p></li>
  <li><p>Even if it is not able to rollback tx for 7th record it does not processes 8th,9th,10th record at all and job is STOPPED.</p></li>
  </ol>
  
  <p>Note: ItemProcessor implementation is calling services (@Service annotated) which are marked as transactional using @Transactional(readOnly=false) annotation.</p>
  
  <p>Please suggest the solution. </p>
  
  <p>ItemProcessor Code Below</p>
  
  <pre><code>public Long process(LoanApplication loanApplication)throws Exception {
      Long createLoan = null;
      LoanStatus loanStatus = null;
      StaffUser user = staffUserService.getStaffUserByName(Constants.AUTO_USER);
      String notes = null;
      try{
          try{
  
              loanValidationService.validate(loanApplication);
  
              loanStatus=LoanStatus.U;
  
          }catch(LoanValidationException e){
              loanStatus=LoanStatus.UC;
              notes=e.getMessage();
          }
  
          dataLoadLoanManagementService.setText(notes);
          createLoan = dataLoadLoanManagementService.createLoan(loanApplication, user,loanStatus);
      }catch(Exception le){
          logger.error("Error creating the loan application ;  Parent Application Ref : " 
              + loanApplication
              + " with status as " +(loanStatus!=null ?loanStatus.getStatus():loanStatus)
              +"\n"
              +" School Ref :"
              + loanApplication.getSchoolRef()
              +"\n"
              +" Client Details :"
              +loanApplication.getClientDetails()
              + "Error Details: " + ExceptionUtils.getStackTrace(le));
  
  
      }   
      return createLoan;
  }
  </code></pre>
  
  <hr>
  
  <p>Even with Skippable Exception Classes configured this does not work.<br>
  To explain bit more i get Persistence Exception in Item Processor and i catch it , therefore Spring batch executes the writer but after writer execution i get the exception below</p>
  
  <pre><code>INFO  06-21 11:38:00 Commit failed while step execution data was already updated. Reverting to old version.  (TaskletStep.java:342) 
  ERROR 06-21 11:38:00 Rolling back with transaction in unknown state  (TaskletStep.java:351) 
  ERROR 06-21 11:38:00 Encountered an error executing the step  (AbstractStep.java:212)
  org.springframework.transaction.TransactionSystemException: Could not commit JPA   transaction; nested exception is javax.persistence.RollbackException: Transaction marked as rollbackOnly
  </code></pre>