javahibernateexceptiondaostaleobjectstate

Not able to catch org.hibernate.StaleObjectStateException


Can we not catch the StaleObjectStateException? I have catch block with StaleObjectStateException , so when method is throwing this error it is coming to this catch block but still throwing the same exception again. I know the reason why this exception is coming but i want to catch the exceptiion and proceed further.

[13/05/15 10:00:07:751 BST] 00000053 TaskUtils$Log E org.springframework.scheduling.support.TaskUtils$LoggingErrorHandler handleError Unexpected error occurred in scheduled task.
                                     org.springframework.orm.hibernate3.HibernateOptimisticLockingFailureException: Object of class [com.lgs.mem.model.MemService] with identifier [3038]: optimistic locking failed; nested exception is org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [com.lgs.mem.model.MemService#3038]
                    at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:676)
                    at org.springframework.orm.hibernate3.SpringSessionSynchronization.translateException(SpringSessionSynchronization.java:160)
                    at org.springframework.orm.hibernate3.SpringSessionSynchronization.beforeCommit(SpringSessionSynchronization.java:148)
                    at org.springframework.transaction.support.TransactionSynchronizationUtils.triggerBeforeCommit(TransactionSynchronizationUtils.java:95)
                    at org.springframework.transaction.support.AbstractPlatformTransactionManager.triggerBeforeCommit(AbstractPlatformTransactionManager.java:927)
                    at org.springframework.transaction.jta.WebSphereUowTransactionManager.access$4(WebSphereUowTransactionManager.java:1)
                    at org.springframework.transaction.jta.WebSphereUowTransactionManager$UOWActionAdapter.run(WebSphereUowTransactionManager.java:338)
                    at com.ibm.ws.uow.EmbeddableUOWManagerImpl.runUnderNewUOW(EmbeddableUOWManagerImpl.java:786)
                    at com.ibm.ws.uow.EmbeddableUOWManagerImpl.runUnderUOW(EmbeddableUOWManagerImpl.java:365)
                    at org.springframework.transaction.jta.WebSphereUowTransactionManager.execute(WebSphereUowTransactionManager.java:281)
                    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:127)
                    at at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
    at $Proxy156.updateRetryCountAndTSP(Unknown Source)
    at com.lgs.mem.service.impl.MemDAOServiceImpl.updateRetryCountAndTSP(MemDAOServiceImpl.java:181)
    at com.lgs.mem.scheduler.MEMRecoveryScheduler.processFailedNicheRequests(MEMRecoveryScheduler.java:82)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:60)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
    at java.lang.reflect.Method.invoke(Method.java:611)
    at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:64)
    at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:53)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:452)
    at java.util.concurrent.FutureTask$Sync.innerRunAndReset(FutureTask.java:328)
    at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:161)
    at org.springframework.scheduling.commonj.TimerManagerTaskScheduler$TimerScheduledFuture.timerExpired(TimerManagerTaskScheduler.java:112)
    at org.springframework.scheduling.commonj.TimerManagerTaskScheduler$ReschedulingTimerListener.timerExpired(TimerManagerTaskScheduler.java:165)
    at com.ibm.ws.asynchbeans.timer.TimerImpl.callListenerMethod(TimerImpl.java:361)
    at com.ibm.ws.asynchbeans.timer.GenericTimer.run(GenericTimer.java:228)
    at com.ibm.ws.asynchbeans.J2EEContext$RunProxy.run(J2EEContext.java:265)
    at java.security.AccessController.doPrivileged(AccessController.java:252)
    at javax.security.auth.Subject.doAs(Subject.java:495)
    at com.ibm.websphere.security.auth.WSSubject.doAs(WSSubject.java:132)
    at com.ibm.websphere.security.auth.WSSubject.doAs(WSSubject.java:90)
    at com.ibm.ws.asynchbeans.J2EEContext$DoAsProxy.run(J2EEContext.java:336)
    at java.security.AccessController.doPrivileged(AccessController.java:280)
    at com.ibm.ws.asynchbeans.J2EEContext.run(J2EEContext.java:1174)
    at com.ibm.ws.asynchbeans.timer.TimerImpl.runListenerAsCJWork(TimerImpl.java:490)
    at com.ibm.ws.asynchbeans.am._Alarm.fireAlarm(_Alarm.java:333)
    at com.ibm.ws.asynchbeans.am._Alarm.run(_Alarm.java:230)
    at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1659)
Caused by: org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [com.lgs.mem.model.MemService#6768]
    at org.hibernate.persister.entity.AbstractEntityPersister.check(AbstractEntityPersister.java:1950)
    at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2595)
    at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:2495)
    at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2822)
    at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:113)
    at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:273)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:265)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:185)
    at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
    at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
    at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
    at org.springframework.orm.hibernate3.SpringSessionSynchronization.beforeCommit(SpringSessionSynchronization.java:145)
    ... 38 more

Solution

  • Yes you can catch the exception.

    Problems like this can occur if the exception does not propagate to the place that you are trying to catch it. For example:

    Judging from the stacktrace, I'd say that it is the second explanation. The original exception has been caught and a HibernateOptimisticLockingFailureException has been created that wraps it. It is the latter exception you are seeing.

    If you want to specifically handle the original exception, you probably need to catch HibernateOptimisticLockingFailureException (or a superclass), and then call the exception object's getMostSpecificCause() to extract the original exception for diagnosis.