I am implementing Spring batch using chunk model. Item reader to read from the list, Item processor to perform business logic and Item writer to finally write to database.
Batch processing works fine when configuring chunk size as 1 but ran into OptimisticLockingFailureException while increasing chunk size. org.springframework.dao.OptimisticLockingFailureException: Attempt to update step execution id=XXXXXX with wrong version (X), where current version is Y.
Here is my Spring batch configuration.
@Bean(BatchJob.BATCH_JOB)
public Job importJob(@Qualifier(BatchJob.READER) ItemReader<Details> reader,
@Qualifier(BatchJob.WRITER) ItemWriter<Details> writer,
@Qualifier(BatchJob.PROCESSOR) ItemProcessor<Details,Details> processor,
@Qualifier(BatchJob.TASK_EXECUTOR) TaskExecutor taskExecutor) {
final Step writeToDatabase = stepBuilderFactory.get(BatchJob.BATCH_STEP)
.<Details, Details>chunk(chunkSize)
.reader(reader)
.processor(processor)
.writer(writer)
.transactionManager(transactionManager)
.taskExecutor(taskExecutor)
.throttleLimit(throttleLimit)
.build();
return jobBuilderFactory.get(BatchJob.JOB_BUILDER_FACTORY)
.incrementer(new RunIdIncrementer())
.start(writeToDatabase)
.build();
}
I have my doubt on ItemProcessor as empty ItemProcessor with no operation works fine even with higher value of chunk size. @Transactional(propagation = Propagation.REQUIRES_NEW) has been used in our business logic.
Can it be because of the transaction management of Spring batch and that used in our business logic or any other possible reasons?
Is there any advantage of using spring batch with chunk size set as 1 and performing same business logic by normal loop one by one.