Spring Batch Exception in Chunk

I have a spring batch job which has multiple steps.

Step 1 : Loads 10 records from the database. (Tasklet does that job)

Step 2: Chunk Oriented processing in configured here using ItemReader,ItemProcessor,ItemWriter implementations with commit -interval =1

Now as I understand , for every record this would happen

Begin Transaction ( Read – Process – Write ) Commit Tx

  1. 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

  2. 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.

Note: ItemProcessor implementation is calling services (@Service annotated) which are marked as transactional using @Transactional(readOnly=false) annotation.

Please suggest the solution.

ItemProcessor Code Below

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;
}

Even with Skippable Exception Classes configured this does not work.
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

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

Chunk reading in Spring Batch – not only chunk writing

My assumption In my understanding chunk oriented processing in Spring Batch helps to me efficiently process multiple items in a single transaction. This includes efficient use of interfaces from ext

Store huge data at Chunk Level in Spring batch

I am new to spring and I am not having much knowledge about Spring ,please help me to solve this out .My use case is : we are using Spring batch with chunk oriented processing to process data. At the

How to use chunk processing with Spring Batch?

I’m using Spring Batch for the first time. I tried some examples and read through documentation. But I have still questions: Can I skip one phase in chunk oriented processing? For example: I fetch da

Spring Batch – Decrypt before Chunk Read

I am using spring batch and need some advice on how to proceed…. I have an encrypted file that will need to be decrypted and stored in memory (not to a file). Once decrypted, I would like to use a c

Spring Batch How to set time interval between each call in a Chunk tasklet

Team, I am doing a technical poc for reading records from a flat file and inserting the data to database. I am using chunk task and running this job using spring batch admin successfully. I have to im

How to call another step within an chunk process in Spring Batch?

Is the following possible/advisable with Spring Batch? I have a csv file, say foo.csv. I set up a chunk process that reads the file into Foo objects and then processes them into Bar objects. Finally t

Spring Batch File Writer Exception handling

I have a Spring Batch process which has following kind of code. <step id=step1 xmlns=http://www.springframework.org/schema/batch> <tasklet allow-start-if-complete=true> <chunk rea

Spring Batch. How to limit the no of executions of a chunk

I started to use spring batch very recently. Can any body tell me how to limit the no of execution of a chunk (i.e. invocation of ItemReader and ItemWrite) within a tasklet. I set the allow-start-if-c

spring batch – Partition Step to rollback all the previous chunk commit ‘s, when chunk fails

I am using spring batch to process multiple files using MultiResourcePartitioner and all the itemreader and writers are in step scope.Each step runs individual files and commits to database at interva

How to throw exception from spring batch processor process() method to Spring batch job started method?

I am having the Web-service method to start spring batch job.If any exception occurred in spring batch processing control is coming back till processor process method. But i need the controller to cam

Answers

For both of your questions, skipping the exception occured during the prosessor phase will solve your problems.

This may be configured by skippable-exception-classes element if you know the root cause of the exception. For example, if you get a divide by zero exception during the processor phase and do want to neglect it, a sample configuration might be:

<chunk reader="reader" processor="processor" writer="writer" 
commit-interval="1" >
<skippable-exception-classes>
<include class="java.lang.ArithmeticException" />
</skippable-exception-classes>
</chunk>

Since the given exception class and its subclasses will be skipped, you may even try java.lang.Exception

check transaction propatation option at loanValidationService.validate

Guess from “Transaction marked as rollbackOnly”, “Commit failed while step execution data was already updated”

Current transaction was rollbacked and parent Transaction should be commit but transaction already over.

if chunk’s transaction is same at LoanValidationService.valite

change propagation option to REQUIRES_NEW

following article may help to understand metadata’s transaction. http://blog.codecentric.de/en/2012/03/transactions-in-spring-batch-part-1-the-basics/

    class LoanValidationServiceImpl implements LoanValidationService {
        @Trasnactional(propagation=REQUIRES_NEW, rollbackFor=Exception.class)
        validate(LoanApplication loanApplication) {
             // business logic
        }
    }