3 Replies Latest reply on May 18, 2006 8:45 PM by 吴奕雄

    how to catch the OptimisticLockException in ejb3

    吴奕雄 Newbie

      when the table item is not exist or delete by the otherone ,i need to catch
      the OptimisticLockException for some reson.but i can't catch it;
      the OptimisticLockException is throw after the delete function finsih and throw by interpet .so how can't i catch the OptimisticLockException

      code:
      @Stateful
      public class MessageManagerBean implements Serializable, MessageManager {
      private static final long serialVersionUID = 3078479285539612928L;

      //a popj ejb3 entity ,map to db "message" table
      private Message message;

      @PersistenceContext(type = TRANSACTION)
      private EntityManager em;

      public String delete() {
      try {
      em.remove(message);//there is not message in "message"!!
      message = null;
      } catch (OptimisticLockException e) {
      e.printStackTrace();//need some process//catch nothing
      return null;
      }
      System.out.println("pass");//i seen success!

      return "deleted";
      }
      }

      after the session bean function finish hibernate throw OptimisticLockException

        • 1. Re: how to catch the OptimisticLockException in ejb3
          吴奕雄 Newbie

          17:11:31,171 INFO [STDOUT] pass
          17:11:31,187 INFO [STDOUT] Hibernate: delete from Message where id=?
          17:11:31,187 ERROR [AbstractBatcher] Exception executing batch:
          org.hibernate.StaleStateException: Batch update returned unexpected row count from update: 0 actual row count: 0 expected: 1
          at org.hibernate.jdbc.BatchingBatcher.checkRowCount(BatchingBatcher.java:93)
          at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:79)
          at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:58)
          at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:242)
          at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:235)
          at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:144)
          at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:297)
          at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
          at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:988)
          at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:337)
          at org.hibernate.ejb.AbstractEntityManagerImpl$1.beforeCompletion(AbstractEntityManagerImpl.java:473)
          at org.jboss.tm.TransactionImpl.doBeforeCompletion(TransactionImpl.java:1488)
          at org.jboss.tm.TransactionImpl.beforePrepare(TransactionImpl.java:1107)
          at org.jboss.tm.TransactionImpl.commit(TransactionImpl.java:321)
          at org.jboss.aspects.tx.TxPolicy.endTransaction(TxPolicy.java:175)
          at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:87)
          at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:192)
          at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
          at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:76)
          at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
          at org.jboss.ejb3.stateful.StatefulInstanceInterceptor.invoke(StatefulInstanceInterceptor.java:81)
          at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
          at org.jboss.aspects.security.AuthenticationInterceptor.invoke(AuthenticationInterceptor.java:78)
          at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
          at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:47)
          at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
          at org.jboss.ejb3.stateful.StatefulContainer.localInvoke(StatefulContainer.java:199)
          at org.jboss.ejb3.stateful.StatefulLocalProxy.invoke(StatefulLocalProxy.java:98)
          at $Proxy232.delete(Unknown Source)
          at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
          at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
          at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
          at java.lang.reflect.Method.invoke(Method.java:585)
          at org.apache.myfaces.el.MethodBindingImpl.invoke(MethodBindingImpl.java:129)
          at org.apache.myfaces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:63)
          at javax.faces.component.UICommand.broadcast(UICommand.java:106)
          at javax.faces.component.UIData.broadcast(UIData.java:338)
          at javax.faces.component.UIViewRoot._broadcastForPhase(UIViewRoot.java:90)
          at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:164)
          at org.apache.myfaces.lifecycle.LifecycleImpl.invokeApplication(LifecycleImpl.java:316)
          at org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:86)
          at javax.faces.webapp.FacesServlet.service(FacesServlet.java:106)
          at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
          at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
          at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
          at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
          at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
          at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
          at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
          at org.jboss.web.tomcat.security.CustomPrincipalValve.invoke(CustomPrincipalValve.java:54)
          at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:174)
          at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:74)
          at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
          at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
          at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
          at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
          at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
          at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
          at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
          at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:112)
          at java.lang.Thread.run(Thread.java:595)
          17:11:31,187 ERROR [AbstractFlushingEventListener] Could not synchronize database state with session
          org.hibernate.StaleStateException: Batch update returned unexpected row count from update: 0 actual row count: 0 expected: 1
          at org.hibernate.jdbc.BatchingBatcher.checkRowCount(BatchingBatcher.java:93)
          at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:79)
          at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:58)
          at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:242)
          at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:235)
          at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:144)
          at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:297)
          at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
          at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:988)
          at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:337)
          at org.hibernate.ejb.AbstractEntityManagerImpl$1.beforeCompletion(AbstractEntityManagerImpl.java:473)
          at org.jboss.tm.TransactionImpl.doBeforeCompletion(TransactionImpl.java:1488)
          at org.jboss.tm.TransactionImpl.beforePrepare(TransactionImpl.java:1107)
          at org.jboss.tm.TransactionImpl.commit(TransactionImpl.java:321)
          at org.jboss.aspects.tx.TxPolicy.endTransaction(TxPolicy.java:175)
          at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:87)
          at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:192)
          at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
          at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:76)
          at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
          at org.jboss.ejb3.stateful.StatefulInstanceInterceptor.invoke(StatefulInstanceInterceptor.java:81)
          at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
          at org.jboss.aspects.security.AuthenticationInterceptor.invoke(AuthenticationInterceptor.java:78)
          at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
          at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:47)
          at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
          at org.jboss.ejb3.stateful.StatefulContainer.localInvoke(StatefulContainer.java:199)
          at org.jboss.ejb3.stateful.StatefulLocalProxy.invoke(StatefulLocalProxy.java:98)
          at $Proxy232.delete(Unknown Source)
          at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
          at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
          at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
          at java.lang.reflect.Method.invoke(Method.java:585)
          at org.apache.myfaces.el.MethodBindingImpl.invoke(MethodBindingImpl.java:129)
          at org.apache.myfaces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:63)
          at javax.faces.component.UICommand.broadcast(UICommand.java:106)
          at javax.faces.component.UIData.broadcast(UIData.java:338)
          at javax.faces.component.UIViewRoot._broadcastForPhase(UIViewRoot.java:90)
          at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:164)
          at org.apache.myfaces.lifecycle.LifecycleImpl.invokeApplication(LifecycleImpl.java:316)
          at org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:86)
          at javax.faces.webapp.FacesServlet.service(FacesServlet.java:106)
          at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
          at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
          at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
          at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
          at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
          at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
          at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
          at org.jboss.web.tomcat.security.CustomPrincipalValve.invoke(CustomPrincipalValve.java:54)
          at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:174)
          at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:74)
          at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
          at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
          at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
          at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
          at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
          at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
          at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
          at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:112)
          at java.lang.Thread.run(Thread.java:595)
          17:11:31,187 ERROR [[Faces Servlet]] Servlet.service() for servlet Faces Servlet threw exception
          javax.faces.FacesException: Error calling action method of component with id _id0:_id2_1:_id13
          at org.apache.myfaces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:74)
          at javax.faces.component.UICommand.broadcast(UICommand.java:106)
          at javax.faces.component.UIData.broadcast(UIData.java:338)
          at javax.faces.component.UIViewRoot._broadcastForPhase(UIViewRoot.java:90)
          at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:164)
          at org.apache.myfaces.lifecycle.LifecycleImpl.invokeApplication(LifecycleImpl.java:316)
          at org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:86)
          at javax.faces.webapp.FacesServlet.service(FacesServlet.java:106)
          at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
          at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
          at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
          at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
          at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
          at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
          at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
          at org.jboss.web.tomcat.security.CustomPrincipalValve.invoke(CustomPrincipalValve.java:54)
          at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:174)
          at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:74)
          at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
          at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
          at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
          at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
          at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
          at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
          at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
          at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:112)
          at java.lang.Thread.run(Thread.java:595)
          Caused by: javax.faces.el.EvaluationException: Exception while invoking expression #{messageManager.delete}
          at org.apache.myfaces.el.MethodBindingImpl.invoke(MethodBindingImpl.java:153)
          at org.apache.myfaces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:63)
          ... 26 more
          Caused by: java.lang.RuntimeException: org.jboss.tm.JBossRollbackException: Unable to commit, tx=TransactionImpl:XidImpl[FormatId=257, GlobalId=kindy/18, BranchQual=, localId=18] status=STATUS_NO_TRANSACTION; - nested throwable: (javax.persistence.OptimisticLockException: org.hibernate.StaleStateException: Batch update returned unexpected row count from update: 0 actual row count: 0 expected: 1)
          at org.jboss.aspects.tx.TxPolicy.handleEndTransactionException(TxPolicy.java:198)
          at org.jboss.aspects.tx.TxPolicy.endTransaction(TxPolicy.java:180)
          at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:87)
          at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:192)
          at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
          at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:76)
          at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
          at org.jboss.ejb3.stateful.StatefulInstanceInterceptor.invoke(StatefulInstanceInterceptor.java:81)
          at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
          at org.jboss.aspects.security.AuthenticationInterceptor.invoke(AuthenticationInterceptor.java:78)
          at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
          at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:47)
          at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
          at org.jboss.ejb3.stateful.StatefulContainer.localInvoke(StatefulContainer.java:199)
          at org.jboss.ejb3.stateful.StatefulLocalProxy.invoke(StatefulLocalProxy.java:98)
          at $Proxy232.delete(Unknown Source)
          at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
          at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
          at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
          at java.lang.reflect.Method.invoke(Method.java:585)
          at org.apache.myfaces.el.MethodBindingImpl.invoke(MethodBindingImpl.java:129)
          ... 27 more
          Caused by: org.jboss.tm.JBossRollbackException: Unable to commit, tx=TransactionImpl:XidImpl[FormatId=257, GlobalId=kindy/18, BranchQual=, localId=18] status=STATUS_NO_TRANSACTION; - nested throwable: (javax.persistence.OptimisticLockException: org.hibernate.StaleStateException: Batch update returned unexpected row count from update: 0 actual row count: 0 expected: 1)
          at org.jboss.tm.TransactionImpl.commit(TransactionImpl.java:369)
          at org.jboss.aspects.tx.TxPolicy.endTransaction(TxPolicy.java:175)
          ... 46 more
          Caused by: javax.persistence.OptimisticLockException: org.hibernate.StaleStateException: Batch update returned unexpected row count from update: 0 actual row count: 0 expected: 1
          at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:546)
          at org.hibernate.ejb.AbstractEntityManagerImpl$1.beforeCompletion(AbstractEntityManagerImpl.java:482)
          at org.jboss.tm.TransactionImpl.doBeforeCompletion(TransactionImpl.java:1488)
          at org.jboss.tm.TransactionImpl.beforePrepare(TransactionImpl.java:1107)
          at org.jboss.tm.TransactionImpl.commit(TransactionImpl.java:321)
          ... 47 more
          Caused by: org.hibernate.StaleStateException: Batch update returned unexpected row count from update: 0 actual row count: 0 expected: 1
          at org.hibernate.jdbc.BatchingBatcher.checkRowCount(BatchingBatcher.java:93)
          at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:79)
          at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:58)
          at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:242)
          at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:235)
          at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:144)
          at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:297)
          at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
          at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:988)
          at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:337)
          at org.hibernate.ejb.AbstractEntityManagerImpl$1.beforeCompletion(AbstractEntityManagerImpl.java:473)
          ... 50 more