Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rollback all steps in Spring Batch

I created a job which splits my file into small chunks and all these chunks are read in the separated steps. For ex. 3 steps are finished without any errors and the records are commited to the database, but if 4th step fails I need to rollback all records from previous step. Is it possible to rollback them somehow?

Or perhaps there is a possibility to commit all records only when the last step was finished correctly? (But here is problem with large files)

like image 872
Shendor Avatar asked Nov 15 '13 14:11

Shendor


People also ask

What is chunking in Spring Batch?

Spring Batch uses chunk oriented style of processing which is reading data one at a time, and creating chunks that will be written out within a transaction. The item is read by ItemReader and passed onto ItemProcessor, then it is written out by ItemWriter once the item is ready.

How do you restart a step in Spring Batch?

– Open Spring Tool Suite, on main menu, choose File->New->Spring Starter Project, input project info. Press Next then Finish, a Spring Boot project will be created successfully.

How do you stop a step in Spring Batch?

To terminate a step you have to return null from ItemReader. read() so when you process an old object you can set a flag into step-execution context and use it to: prevent other items of current chunk to be processed. prevent chunk processed item writing.


1 Answers

Don't play with transaction while using spring batch; due to its transactional nature is a really bad idea manually manage transaction.
See Transaction Management in Spring batch or Spring batch - One transaction over whole Job for further explanation

like image 199
Luca Basso Ricci Avatar answered Oct 18 '22 11:10

Luca Basso Ricci