commit-interval in Spring batch and dealing with rollbacks

balteo picture balteo · Jul 11, 2012 · Viewed 17.2k times · Source

My question relates to Spring batch and transactions.

Say I've chosen a commit-interval of 50 for one of my steps.

Also suppose I have 1000 records in all and amongst those records one will cause the itemWriter to fail thereby causing a rollback of the entire chunk (50 records in my example).

What are the stategies to make sure that the 49 valid records are written to database after the job has completed (and ignored the problematic chunk)?

Answer

balteo picture balteo · Jul 11, 2012

After some researching, I came up with the following:

If an item writer fails to commit a chunk (here 50 items) thereby causing a rollback, Spring Batch will rerun each item of the problematic chunk individually with one commit/transaction for each item.

Therefore, all 49 items will be present in database except the one item that caused Spring Batch to roll back the chunk.