简体   繁体   中英

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:

 @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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM