Error with second save operation - Optimistic lock exception on saving entity

Viewed 220

My transactions are like this -

  1. findById(id) -- version 1
  2. do some operations on object -- version 1
  3. save(data) and return latest object -- version 2
  4. again perform some action on object -- version 2
  5. save(data) object in database again (version should be 3 after update)

In some cases, i'm getting Optimistic lock exception at point 5 even though db version and update object version are correct.

mongo, java

org.springframework.dao.OptimisticLockingFailureException:
   at org.springframework.data.mongodb.core.MongoTemplate.doSaveVersioned (MongoTemplate.java:1458)
   at org.springframework.data.mongodb.core.MongoTemplate.save (MongoTemplate.java:1420)
   at org.springframework.data.mongodb.repository.support.SimpleMongoRepository.save (SimpleMongoRepository.java:87)
   at sun.reflect.GeneratedMethodAccessor237.invoke
   at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
   at java.lang.reflect.Method.invoke (Method.java:498)
   at org.springframework.data.repository.core.support.RepositoryComposition$RepositoryFragments.invoke (RepositoryComposition.java:371)
   at org.springframework.data.repository.core.support.RepositoryComposition.invoke (RepositoryComposition.java:204)
   at org.springframework.data.repository.core.support.RepositoryFactorySupport$ImplementationMethodExecutionInterceptor.invoke (RepositoryFactorySupport.java:657)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed (ReflectiveMethodInvocation.java:186)
   at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke (RepositoryFactorySupport.java:621)
   at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke (RepositoryFactorySupport.java:605)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed (ReflectiveMethodInvocation.java:186)
   at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke (DefaultMethodInvokingMethodInterceptor.java:80)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed (ReflectiveMethodInvocation.java:186)
   at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke (ExposeInvocationInterceptor.java:95)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed (ReflectiveMethodInvocation.java:186)
   at org.springframework.aop.framework.JdkDynamicAopProxy.invoke (JdkDynamicAopProxy.java:212)
   at com.sun.proxy.$Proxy239.save
0 Answers
Related