question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

OptimisticLockingFailureException updating step execution after commit failure

See original GitHub issue

Quinton McCombs opened BATCH-1767 and commented

It appears that if the commit fails, spring batch will get an OptimisticLockingFailureException when it tries to revert the changes to the step execution. In my particular case, I have a callback through EclipseLink to update history tables before a transaction is committed. If a failure occurs during this callback, the commit fails.

From looking through the code and the attached log file, the step execution is updated and committed before the main transaction is committed. When the commit fails, the old values for the step execution (including version) are updated to the values before the chuck started. When control returns to AbstractStep.execute(), the OptimisticLockingFailureException is thrown when the step execution is updated with the failed status because the new version had already been committed to the database.

2011-07-06 17:40:29,494 ERROR SimpleAsyncTaskExecutor-1 [org.springframework.batch.core.step.AbstractStep] Encountered an error saving batch meta data. This job is now in an unknown state and should not be restarted.
org.springframework.dao.OptimisticLockingFailureException: Attempt to update step execution id=3225 with wrong version (35), where current version is 36
   at org.springframework.batch.core.repository.dao.JdbcStepExecutionDao.updateStepExecution(JdbcStepExecutionDao.java:185)
   at org.springframework.batch.core.repository.support.SimpleJobRepository.update(SimpleJobRepository.java:171)
   at sun.reflect.GeneratedMethodAccessor130.invoke(Unknown Source)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:597)
   at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
   at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
   at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
   at $Proxy77.update(Unknown Source)
   at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:244)
   at org.springframework.batch.core.job.SimpleStepHandler.handleStep(SimpleStepHandler.java:135)
   at org.springframework.batch.core.job.flow.JobFlowExecutor.executeStep(JobFlowExecutor.java:61)
   at org.springframework.batch.core.job.flow.support.state.StepState.handle(StepState.java:60)
   at org.springframework.batch.core.job.flow.support.SimpleFlow.resume(SimpleFlow.java:144)
   at org.springframework.batch.core.job.flow.support.SimpleFlow.start(SimpleFlow.java:124)
   at org.springframework.batch.core.job.flow.support.state.SplitState$1.call(SplitState.java:91)
   at org.springframework.batch.core.job.flow.support.state.SplitState$1.call(SplitState.java:89)
   at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
   at java.util.concurrent.FutureTask.run(FutureTask.java:138)
   at java.lang.Thread.run(Thread.java:662)

Affects: 2.1.7

Attachments:

Referenced from: pull request https://github.com/spring-projects/spring-batch/pull/591

23 votes, 31 watchers

Issue Analytics

  • State:open
  • Created 12 years ago
  • Reactions:1
  • Comments:49 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
ingleivjcommented, Jan 11, 2021

@snagunoori As mentioned in my previous comment, the fix will be merged when we gather enough feedback from the community. For now, only one person tested the fix and gave feedback. If you want to help moving things forward, I invite you to give the PR a try and share your feedback as well (on the issue related to the PR, not here).

Hi, at my workplace we have seen this issue now and then over the last few years (currently on Spring 4.3.29, Spring Batch 3.0.10), but usually it has not been consistently present or we have worked around it. Last week it started to hit all the time, so I tried a patched version today - with success!

0reactions
thouet-sigmacommented, Dec 23, 2021

@snagunoori As mentioned in my previous comment, the fix will be merged when we gather enough feedback from the community. For now, only one person tested the fix and gave feedback. If you want to help moving things forward, I invite you to give the PR a try and share your feedback as well (on the issue related to the PR, not here).

Hello, We faced this issue since a long time. I implemented your PR and released a version (based on 3.0.10) about 3 years ago at my work. Since, we don’t get anymore this issue 😃

Could you consider to release this “soon” ? That would allow us to migrate from 3.0.10 to 4.X

Read more comments on GitHub >

github_iconTop Results From Across the Web

OptimisticLockingFailureException updating step execution ...
I am performing some integration testing for my application in which I am trying to force an exception whenever there is an update/insert ......
Read more >
JPA optimistic lock exception in Java Development
OptimisticLockException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect). The problem is a JPA ...
Read more >
6.4. Exclusive Control - terasoluna.org
When concurrency for the same data is more and update process takes longer time. Possibility of a process failure due to process results...
Read more >
5 Common Hibernate Exceptions and How to Fix Them
This works fine until I try to commit the second transaction and Hibernate checks for concurrent updates of this Author entity. In a...
Read more >
Spring Batch - Reference Documentation
Execution context serialization updates. ... Failing a Step . ... series of steps that can be implemented by using the following standard building...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found