spring-batch - 项目写入期间意外的 rollbackCount 和 shouldSkip() 的调用

标签 spring-batch

Spring 文档(第 46 页,第:5.1.7 节)说:

By default, regardless of retry or skip, any exceptions thrown from the ItemWriter will cause the transaction controlled by the Step to rollback. If skip is configured as described above, exceptions thrown from the ItemReader will not cause a rollback.



我的提交间隔设置为 10。所以我对上段的理解是,如果他们在读取 10 块中的第 7 条记录时出错,则该项目将被跳过,itemReader 将提前发送正确的 9 条记录。

但是,如果第 7 条记录在写入过程中出错 - 将不会写入 10 条记录并且会发生回滚。

但是,当我包含在我的 skipPolicy 中抛出的错误时,itemWriter 将剩余的 9 条记录写入数据库,跳过出错的一条记录。这与上述内容相矛盾。

任何人都可以解释“在项目编写过程中跳过”的概念。

此外,即使抛出单个错误,我也会收到以下信息:

在我的 shouldSkip(Object, Throwable) 方法中,SkipCount 为 -1 两次,然后为 0 一次,再次为 -1 一次。 - 我没有得到这种行为。

回滚计数也是 2 - 这是什么意思?为什么是 2 ?

@michael 你能不能用一些场景来解释这种行为!!

就像“我正在从文件中读取 20 条记录并在一些处理后写入数据库。我为某些异常设置了跳过策略。如果在读取、处理、写入期间发生异常会发生什么——块将如何已提交、默认重试如何工作、如何更新计数等等......”

这对我来说真的是一个很大的帮助,因为我仍然对这种行为感到困惑..

最佳答案

从您的用例描述看来,您混合了不同的概念。

你描述了一个 skip scenario但您似乎希望跳过应该像 no-rollback 一样工作设想。

来自 spring 批处理文档

skip :

errors encountered while processing should not result in Step failure, but should be skipped instead



对比 no-rollback :

If skip is configured as described above, exceptions thrown from the ItemReader will not cause a rollback.



用我自己的话来说,skip 的意思是:

如果该步骤在读取/处理/写入期间遇到错误,则当前块将被回滚并且块的每个项目被单独读取/处理/写入 - 没有坏项目。基本上 Spring Batch 回退到坏块的提交率 1,并在坏块之后返回到指定的提交率。

Also rollback count is 2 -- what does it mean ? why it is 2 ?



来自 B.5. BATCH_STEP_EXECUTION

ROLLBACK_COUNT: The number of rollbacks during this execution. Note that this count includes each time rollback occurs, including rollbacks for retry and those in the skip recovery procedure.



(强调我的)

Also even though single error is thrown I am getting the following:

SkipCount as -1 twice, then as 0 once, and again -1 once in my shouldSkip(Object, Throwable) method. -- I am not getting this behavior.



我尝试了一个具有两种配置样式的简单跳过作业,skip-policy 和 skip-limit with skippable-exception,两者在回滚和跳过计数方面的工作方式相同

(步骤元数据没问题,但 shouldSkip(...) 的调用似乎比预期的要多得多)

关于spring-batch - 项目写入期间意外的 rollbackCount 和 shouldSkip() 的调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6729260/

相关文章:

java - 从 ExecutionContext 获取参数

scala - 批处理和函数式编程

java - Spring 批处理 : Create an ItemReader that reads an xml file from a web service

java - 使用监听器捕获 Spring Batch 中步骤中的错误

java - Spring Batch DataReader 在读取项目集合末尾返回额外的空对象?

java - Spring Batch Restart 逻辑如何在 hadoop 作业上工作?

spring-batch - 在作业中执行步骤时遇到错误

java - spring-retry Retryble 注释与 retryTemplate

java - 添加 spring-cloud-starter-dataflow-server-local 会导致错误 : Command line is too long. Shorten command line for ... 或 Spring Boot

transactions - 尽管未定义回滚异常类,但 Spring Batch 确实回滚