0

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.

JDev
  • 1,662
  • 4
  • 25
  • 55
  • Have you tried defining a noRollBack policy in your step builder? `.writer(testWriter).noRetry(Exception.class).noRollback(Exception.class)` Also possible duplicate of: https://stackoverflow.com/questions/57793522/how-do-i-prevent-rollback-when-exception-occurs-in-itemwriter and https://stackoverflow.com/questions/22522681/spring-batch-skip-exception-for-itemwriter – Sara Jun 11 '20 at 01:14

1 Answers1

0

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.

Mahmoud Ben Hassine
  • 28,519
  • 3
  • 32
  • 50