java Spring批处理中的提交间隔和处理回滚
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11429197/
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
commit-interval in Spring batch and dealing with rollbacks
提问by balteo
My question relates to Spring batch and transactions.
我的问题与 Spring 批处理和事务有关。
Say I've chosen a commit-interval of 50for one of my steps.
假设我为我的步骤之一选择了50的提交间隔。
Also suppose I have 1000 records in all and amongst those records one will cause the itemWriter to fail thereby causing a rollback of the entire chunk (50 records in my example).
还假设我总共有 1000 条记录,其中一条会导致 itemWriter 失败,从而导致整个块的回滚(在我的示例中为 50 条记录)。
What are the stategies to make sure that the 49 valid records are written to database after the job has completed (and ignored the problematic chunk)?
确保在作业完成(并忽略有问题的块)后将 49 条有效记录写入数据库的策略是什么?
回答by balteo
After some researching, I came up with the following:
经过一番研究,我想出了以下几点:
If an item writer fails to commit a chunk (here 50 items) thereby causing a rollback, Spring Batch will rerun each item of the problematic chunk individually with one commit/transaction for each item.
如果项目编写器未能提交一个块(这里是 50 个项目)从而导致回滚,Spring Batch 将单独重新运行有问题的块的每个项目,每个项目都有一个提交/事务。
Therefore, all 49 items will be present in database except the one item that caused Spring Batch to roll back the chunk.
因此,除了导致 Spring Batch 回滚块的一项之外,所有 49 项都将出现在数据库中。
回答by GMsoF
We focused on skipping items during the reading phase, but the skip configuration also applies to the processing and writing phases of a chunk-oriented step. Spring Batch doesn't drive a chunk-oriented step the same way when a skippable exception is thrown in the reading, processing, or writing phase.
When an item reader throws a skippable exception, Spring Batch just calls the read method again on the item reader to get the next item. There's no rollback on the transaction. When an item processor throws a skippable exception, Spring Batch rolls back the transaction of the current chunk and resubmits the read items to the item processor, except for the one that triggered the skippable exception in the previous run. Figure 8.3 shows what Spring Batch does when the item writer throws a skippable exception. Because the framework doesn't know which item threw the exception, it reprocesses each item in the chunk one by one, in its own transaction.
我们专注于在读取阶段跳过项目,但跳过配置也适用于面向块的步骤的处理和写入阶段。当在读取、处理或写入阶段抛出可跳过的异常时,Spring Batch 不会以相同的方式驱动面向块的步骤。
当一个 item reader 抛出一个可跳过的异常时,Spring Batch 只是在 item reader 上再次调用 read 方法来获取下一个 item。事务没有回滚。当项目处理器抛出可跳过的异常时,Spring Batch 会回滚当前块的事务,并将读取的项目重新提交给项目处理器,但在上一次运行中触发了可跳过的异常的除外。图 8.3 显示了当项目编写器抛出一个可跳过的异常时 Spring Batch 会做什么。因为框架不知道哪个项目抛出了异常,所以它在自己的事务中一个一个地重新处理块中的每个项目。
I quote the paragraph from book Spring Batch in Action, Manning
.
我引用书中的段落Spring Batch in Action, Manning
。
It is quite tricky, the rollback behavior is different depend on whether the exception is thrown in reading, processing or writing.
相当棘手,回滚行为因读取、处理或写入时是否抛出异常而异。
Hope this help others.
希望这对其他人有帮助。