Java 如何回滚Spring Batch中的所有步骤

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20003042/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 22:16:08  来源:igfitidea点击:

How to rollback all steps in Spring Batch

javaspringspring-batch

提问by Shendor

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?

我创建了一个作业,将我的文件分成小块,所有这些块都在单独的步骤中读取。例如。3 步完成且没有任何错误,记录已提交到数据库,但如果第 4 步失败,我需要回滚上一步的所有记录。是否有可能以某种方式回滚它们?

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)

或者也许只有在最后一步正确完成时才可能提交所有记录?(但这是大文件的问题)

采纳答案by Luca Basso Ricci

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 batchor Spring batch - One transaction over whole Jobfor further explanation

使用 spring 批处理时不要玩交易;由于其事务性质,手动管理事务是一个非常糟糕的主意。
有关进一步说明,请参阅Spring 批处理中的事务管理Spring 批处理 - 整个作业中的一个事务

回答by user1339772

Not just spring, for any framework if you need to perform atomic operation across multiple read/write to data source, generally all those calls need to be wrapped in a transaction and then either committed or rolled back at the end. Understanding how JTA works goes a long way in identifying how to use framework that handle transactions, more information on JTA can be found here

不只是 spring,对于任何框架,如果您需要跨多个读/写数据源执行原子操作,通常所有这些调用都需要包装在一个事务中,然后在最后提交或回滚。了解 JTA 的工作原理对确定如何使用处理事务的框架大有帮助,可以在此处找到有关 JTA 的更多信息