Saturday, December 11, 2010

OptimisticLockingFailureException Attempt to update step execution id=1 with wrong version (n), where current version is n+1

My Spring batch application was intended to run in Single thread env and concurrent updates were not in its scope.But one of the Jobs ,kept encountering OptimisticLockingFailureException:Attempt to update step execution id=1 with wrong version (1), where current version is 2
On analyzing the Spring Logs found that this was generated by the MapStepExecutionDao.I am using MapJobRepositoryFactoryBean with ResourcelessTransactionManager which is NOT suppose to persist the data .
After testing with few scenarioes found that the exception was thrown only when the batch operation tries to insert duplicate records to the table with constraints.After removing the duplicate data the batch execution was successful.
In this case OptimisticLockingFailureException was misleading ,a more appropriate DB Exception would have be helpful identify the cause.

7 comments:

  1. Thanks, that helped me alot to solve my problem

    ReplyDelete
  2. Hi, I am facing this currently in my spring batch. What do u mean duplicate data? In which table do i need to checj that.., Can you please throw some light on it..,

    ReplyDelete
  3. Hi, I am also getting the same issue.Can you please let me know how can we resolve/avoid this?

    ReplyDelete
    Replies
    1. Hi Panks,
      Check your destination tables for index and unique key constraint.Is this exception thrown in your testing or production environment?

      Delete
  4. Thank you! I'm currently facing this problem, and you hace really helped me

    ReplyDelete
  5. I am facing a similar issue, however I am using the JdbcStepExecutionDao.
    This is my job config:
    @Bean
    public Step dealerProviderUpdateStep(StepBuilderFactory steps, DealerProviderReader reader, DealerProviderProcessor processor,
    DealerProviderWriter writer) {
    return steps.get(dealerProviderUpdateJobName). chunk(100).faultTolerant().reader(reader)
    .processor(processor).writer(writer).build();
    }
    I am struggling to figure out what is causing the issue. Any clues ?

    ReplyDelete
  6. @mubs To resolved optimisticlockingfailureexception please go through the logs and identify the actual exception. Actual root cause will be due to other exception. In my case querty timeout exception was coming. So go through the logs above where optimisticlockingfailureexception occured

    ReplyDelete