You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Batch 6.X中noRollback方法的等价替代方案咨询

Spring Batch 6中FaultTolerantStepBuilder.noRollback方法的替代方案

原FaultTolerantStepBuilder中的noRollback功能并未被移除,而是整合到了统一的异常处理配置机制中。在Spring Batch 6里,你需要通过FaultTolerantStepBuilder的exceptionHandler方法,结合DefaultExceptionHandler的相关API来实现相同效果:

  • 使用DefaultExceptionHandler.noRollbackFor(Class<? extends Throwable> exceptionClass)指定单个无需回滚的异常类型
  • 使用DefaultExceptionHandler.noRollbackForClasses(Class<? extends Throwable>... exceptionClasses)批量指定无需回滚的异常类型

示例代码

@Bean
public Step myFaultTolerantStep(JobRepository jobRepository, PlatformTransactionManager transactionManager) {
    return new StepBuilder("myFaultTolerantStep", jobRepository)
            .<String, String>chunk(10, transactionManager)
            .reader(myItemReader())
            .writer(myItemWriter())
            .faultTolerant()
            .exceptionHandler(new DefaultExceptionHandler()
                    .noRollbackFor(CustomNonRollbackException.class)
                    .noRollbackForClasses(AnotherNonRollbackException.class, ThirdNonRollbackException.class))
            .build();
}

这种设计将回滚规则的配置集中到异常处理器中,让异常处理逻辑更清晰、配置更灵活,替代了原noRollback方法的直接调用。

内容的提问来源于stack exchange,提问作者Svetopolk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 00:05:18