0
votes

I am using spring batch to read data form database and write it to file.
Following are the steps involved:
1. Read data from database
2. Write data to file
3. Update database rows if file write is successful.

According to spring batch documentation and other sources, I can use JdbcBatchItemWriter for updating database entries. But I could not find any mechanism to control commit/rollback while updating the rows.
Say there are 100 rows and an error occurs at 45th row, I need to rollback all the updates.
Is there some way in JdbcBatchItemWriter to explicitly commit or rollback the transaction?

EDIT
The Itemwriter will data to file in chunks. Once a chunk is written, I need to update records in database. The database update is done using JdbcBatchItemWriter. Say 5 chunks are written successfully, and chunk 6 throws error in 45th record, I need to rollback all the chunks. Therefore I need commit/rollback control in JdbcBatchItemWriter.

2

2 Answers

0
votes

commit/rollback or transactional behaviour in general is provided and controlled by the framework, the same applies to fault tolerant steps using skip

Say there are 100 rows and an error occurs at 45th row, I need to rollback all the updates. Is there some way in JdbcBatchItemWriter to explicitly commit or rollback the transaction?

that behaviour is provided automatically by spring batch, it will be rollbacked and - by using skip logic - the other 99 rows will be processed and committed

Chunkoriented processing :

Chunk oriented processing refers to reading the data one at a time, and creating 'chunks' that will be written out, within a transaction boundary. One item is read in from an ItemReader, handed to an ItemProcessor, and aggregated. Once the number of items read equals the commit interval, the entire chunk is written out via the ItemWriter, and then the transaction is committed.

see also:

0
votes

There is no way within Spring Batch by default to roll back a committed transaction. What you'll need to do is add compensating logic in another step that would be called if the step in question failed.