Spring batch - One transaction over whole Job

让人想犯罪 __ 提交于 2019-12-18 05:07:15

问题


I am using Spring-Batch to execute a batch that creates some objects in the database, creates a file from these objects and then sends the file to a FTP server.

Thus, I have 2 steps : One that reads conf from DB, insert into the DB and creates the file ; the second sends the file to the FTP server.

The problem is when there is a problem with the FTP server, I can't rollback the transaction (to cancel the new inserts into the DB).

How can I configure my Job to use just one transaction over the different steps?


回答1:


This is a bad idea due to transactional nature of spring-batch.
IMHO a simple solution should be to mark data saved in step 1 with a token generated when job starts and, if your FTP upload will fail, move to a cleanup step to delete all data with token.




回答2:


A agree with bellabax: this is a bad idea.

But I wouldn't do a 3rd cleanup step because this step may also fail, letting the transaction not rollbacked.

You could mark the inserted entries with a flag that indicates the entries has not yet been sent to the FTP. The 3rd step would switch the flag to indicate that these entries has been sent to the FTP.

Then you just need a cron/batch/4th cleaning step/whatever that would remove all entries that haven't been sent to the FTP



来源:https://stackoverflow.com/questions/19031186/spring-batch-one-transaction-over-whole-job

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!