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.