Home > Software design >  Make 2 writers spring batch transactional
Make 2 writers spring batch transactional

Time:11-17

How to make transactional 2 writers - one JDBC writer and a flat file writer?

When for example the directory to write in doesn't exist I have a fileNotFoundException. But the other JDBC writer already wrote in database.

I tried @Transactional in the writer, but the exception occurred in the compressor.

CodePudding user response:

You need to use a CompositeItemWriter with two item writers. The transaction will be around the composite writer, so if one of the delegates fails the transaction will be rolled back and the other delegate's operation will be reverted as well.

In your case, a failure in writing the file will make the database insertion to be reverted due to the rollback of the transaction.

  • Related