[英]Spring batch: Why transaction is rolling back even if I catching the exception
I am using Spring Batch and my step configuration is as below:我正在使用 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();
}
And in my writer:在我的作家中:
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());
}
});
}
With the code, I expected that if something goes wrong with the batch then I will try individual items and skip the failed ones.使用代码,我希望如果批次出现问题,那么我将尝试单个项目并跳过失败的项目。
However, it is not happening.然而,这并没有发生。 Seems transaction is lost and Is not recovered.似乎事务丢失并且未恢复。
With the code, I expected that if something goes wrong with the batch then I will try individual items and skip the failed ones.使用代码,我希望如果批次出现问题,那么我将尝试单个项目并跳过失败的项目。
The expectation is incorrect.预期是不正确的。 Chunk scanning is triggered when a skippable exception is thrown from the item writer.当 item writer 抛出可跳过的异常时,会触发块扫描。 This does not seem the case from your configuration: you did not configure any skippable exception and your writer does not throw one.从您的配置来看,情况似乎并非如此:您没有配置任何可跳过的异常,并且您的编写器也没有抛出异常。 Your step definition should be something like:您的步骤定义应类似于:
return stepBuilderFactory
.get("TESTING")
// other properties
.skip(MySkippableException.class)
.skipLimit(10)
.build()
And your writer should throw MySkippableException
at some point.你的作者应该在某个时候抛出MySkippableException
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.