【发布时间】:2020-09-30 13:07:40
【问题描述】:
我使用的是 Spring Batch,步骤配置如下:
@Bean
public Step testStep(
JdbcCursorItemReader<TestStep> testStageDataReader,
TestStepProcessor testStepProcessor,
CompositeItemWriter<Writer> testWriter,
PlatformTransactionManager transactionManager,
JobRepository jobRepository) {
return stepBuilderFactory
.get("TESTING")
.<>chunk(100)
.reader(testStageDataReader)
.processor(testStepProcessor)
.writer(testWriter)
.transactionManager(transactionManager)
.repository(jobRepository)
.build();
}
在我的作家中:
void write(List<? extends TestEntity> items) {
try {
testRepository.saveAll(items);
} catch (Exception exp) {
log.error("Exception while writing the batch", Exp);
//If there is an exception try individual items and skip filed ones
items.forEach(eachTask -> {
try {
testRepository.save(eachTask);
} catch (Exception exp) {
logException(exp, eachTask.getId());
}
});
}
使用代码,我预计如果批次出现问题,我会尝试单个项目并跳过失败的项目。
但是,这并没有发生。似乎事务已丢失且未恢复。
【问题讨论】:
-
您是否尝试在步骤生成器中定义 noRollBack 策略?
.writer(testWriter).noRetry(Exception.class).noRollback(Exception.class)也可能重复:stackoverflow.com/questions/57793522/… 和 stackoverflow.com/questions/22522681/… -
请跟进您的问题。我添加了一个答案,如果有帮助请采纳:stackoverflow.com/help/someone-answers
标签: java spring spring-boot spring-batch spring-batch-stream