java - Spring Batch 中是否可以抛出致命的不可回滚异常?

标签 java spring spring-batch

我尝试了 noSkip()、noRetry()、noRollback() 的所有组合,但没有实现。 如果我在没有 noRollback(MyException.class) 的情况下设置步骤,则一旦处理的行引发异常,该步骤就会失败,但它会回滚所有数据库操作。如果我使用 noRollback(MyException.class),则数据库状态将保留,但我不会收到 fatal error 的日志,并且步骤不会中断/失败。

有什么方法可以直观地处理这个问题吗?这是我尝试的最后一个代码

  return this.stepBuilderFactory.get(STEP_PROCESS_ROWS)
            .faultTolerant()
            .noSkip(UnexpectedJobExecutionException.class)
            .noRetry(UnexpectedJobExecutionException.class)
            .noRollback(UnexpectedJobExecutionException.class)
            .reader(myReader)
            .processor(myProcessor)
            .writer(myWriter)
            .build();

最佳答案

Is it possible to throw a fatal non-rollbackable exception in Spring Batch?

是的,使用 FaultTolerantStepBuilder#noRollback正如你所做的那样。 noSkipnoRetry 具有不同的含义。

If I set the step without noRollback(MyException.class), the step fails as soon as the processed row throws the exception, but it rollbacks all db actions.

这是默认行为。如果发生异常,事务将回滚并且该步骤失败。

If I use noRollback(MyException.class), then the db state is persisted, but I get no log of the fatal error and step is not interrupted/failed.

这里没什么奇怪的(我希望):由于事务没有回滚,状态被持久化并且该步骤继续。确保将日志级别设置为调试以获取所有详细信息。

关于java - Spring Batch 中是否可以抛出致命的不可回滚异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60859552/

相关文章:

java - 如何获取正在运行的 JAR 文件的路径?

java - 搜索 SOAP 访问的正确格式

java - HBase java 错误 - 预期 HEADER=HBas 但收到 HEADER=\x00\x00\x01\x0B

java - 如何使用 Spring Template + JDBCTemplate 发送日期列表作为 sql 查询的一部分

java - Spring 批处理 : Read from txt files and return all lines as a single String to the processor

java - 如何将硬编码的 SQL 查询传递到 JdbcBatchItemWriter 中?

java - 添加 mvc 命名空间时 Spring-MVC 不起作用

java - Spring Boot事务管理器建议回滚所有异常

java - 为什么 @Autowired(required=false) 不适用于 @Configuration bean?

spring-batch - Spring-Batch 项目的典型包命名约定是什么?