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
相关产品推荐
相关产品推荐

