0

I have been facing a strange problem lately. I tried to handle the the stale state exception gracefully . But in catch block it still throws the exception . Following is the code snippet

public void saveObject(Object ob){
   try{
      sessionFactory.getCurrentSession().saveOrUpdate(ob);
   }catch(org.springframework.orm.hibernate5.HibernateOptimisticLockingFailureException e){
       object latestObject = // get latest object from db;
       copyFieldsFromObToLatestObject(ob,latestObject);
       // print the version of of both object

    LOGGER.info(" ui_version="+ob.getVersion().longValue()+" 
                 entity_version="+latestObject.getVersion().longValue());
          // the ui _version is less than entity_ version as expected 

        sessionFactory.getCurrentSession().saveOrUpdate(latestObject); // at this line I still get the same optimistic locking exception 

   }
}
 /**

 ob2 is the latest object which contains the correct  version hence copying the fields from previous object to this latest object
**/
private void copyFieldsFromObToLatestObject(ob1,ob2){
    ob2.setA(ob1.getA())..
   so on
}

Can some one take a look at this . I am unable to get the reason why it still throws optimistic locking exception after handling it correctly

EDIT 1 : the stacktrace :

org.springframework.orm.hibernate5.HibernateOptimisticLockingFailureException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1; nested exception is org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1 [INFO] at org.springframework.orm.hibernate5.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:283) [INFO] at org.springframework.orm.hibernate5.HibernateTransactionManager.convertHibernateAccessException(HibernateTransactionManager.java:755) [INFO] at org.springframework.orm.hibernate5.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:590) [INFO] at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:765) [INFO] at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:734) [INFO] at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:518) [INFO] at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:292) [INFO] at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) [INFO] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) [INFO] at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213) [INFO] at com.sun.proxy.$Proxy155.saveOrUpdate(Unknown Source) [INFO] at biz.kaar.common.services.appointment.impl.AppointmentServiceExtension1BOImpl.handleStaleStateException(AppointmentServiceExtension1BOImpl.java:1520) [INFO] at biz.kaar.common.services.appointment.impl.AppointmentServiceExtension1BOImpl.saveSAR(AppointmentServiceExtension1BOImpl.java:845) [INFO] at biz.kaar.common.services.DBServiceImpl.saveSAR(DBServiceImpl.java:382) [INFO] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [INFO] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) [INFO] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [INFO] at java.lang.reflect.Method.invoke(Method.java:498) [INFO] at net.sf.gilead.gwt.PersistentRemoteService.processCall(PersistentRemoteService.java:115) [INFO] at biz.kaar.common.security.AuthorizedGWTServlet.processCall(AuthorizedGWTServlet.java:252) [INFO] at biz.kaar.common.services.RemoteServletWithLogging.processCall(RemoteServletWithLogging.java:90) [INFO] at com.google.gwt.user.server.rpc.RemoteServiceServlet.processPost(RemoteServiceServlet.java:373) [INFO] at com.google.gwt.user.server.rpc.AbstractRemoteServiceServlet.doPost(AbstractRemoteServiceServlet.java:62) [INFO] at javax.servlet.http.HttpServlet.service(HttpServlet.java:707) [INFO] at javax.servlet.http.HttpServlet.service(HttpServlet.java:790) [INFO] at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:812) [INFO] at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1669) [INFO] at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:316) [INFO] at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityIntercep

vipulk10
  • 117
  • 3
  • 15

2 Answers2

1

In the catch block you should catch the exception that is thrown and that you want to process. You catch Optimistic locking exception. I cannot even imagine how this compiles. Try to replace it with name of the exception in stack trace?

Michael Kreutz
  • 1,216
  • 6
  • 9
0

Problem was solved , I was copying the version of associated child entities in the to field as well which lead to older version of child entities being getting saved and hence leading to error

vipulk10
  • 117
  • 3
  • 15