Search code examples
spring-bootspring-batch

how to make each transaction for itemReader and itemProcessor and itemWriter


i want to different transaction for itemReader and itemProcessor and itemWriter

Sample Code :

@Bean
@JobScope
public Step step() {
   return stepBuilderFactory.get("stepName")
          .<I,O>chunk(10)
          .reader(reader)       // commit before processor for status save to WORKING in DB
          .processor(processor) // when processor cause exception then rollback
          .writer(writer)       // when processor cause exception then save to Fail in DB
          .build();
}

is that possible?

i tried these cases

CASE 1: (Not work)


public CustomItemProcessor implements ItemProcessor<I,O>{

   @Transactional(Requires_new)
   public O process(I vo) {
       return insertMethodInProceesor(I);
   }

}

CASE 2: (Not work)

@Transactional(Requires_new)
public void insertMethodInProceesor() {...}

CASE 3: (Working but not my expected)

   return stepBuilderFactory.get("stepName")
          .chunk(10)
          .reader(reader)       
          .processor(processor) 
          .writer(writer)       
          .faulttolerant()
          .skip(Exception.class)
          .build();

Solution

  • This is not possible. All phases of the chunk-oriented processing model are executed within the scope of the same transaction. There are no separate transactions for the reader, processor and writer.