I am using Spring Batch and my step configuration is as below:
@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.