0 Replies Latest reply on Apr 24, 2006 11:52 AM by Didi

    catching exception after duplicate persist (Hibernate)

    Didi Newbie

      Hi,

      I am using EJB3.0 and the integrated EntityManager from Hibernate to persist a UserEntityBeans with the username as unique column. Whenever I insert the same username the second time, I always get an exception

      17:38:08,182 WARN [JDBCExceptionReporter] SQL Error: 0, SQLState: null
      17:38:08,182 ERROR [JDBCExceptionReporter] failed batch
      17:38:08,183 ERROR [AbstractFlushingEventListener] Could not synchronize database state with session
      org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update
       at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:91)
       at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:79)
       at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
       at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:202)
       at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:230)
       at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:140)
       at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:296)
       at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
       at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1009)
       at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:196)
       at org.jboss.ejb3.entity.InjectedEntityManager.flush(InjectedEntityManager.java:166)
       at de.uni_mannheim.informatik.swt.ejb.user.UserFacadeBean.addUser(UserFacadeBean.java:24)
       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.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:109)
       at org.jboss.ejb3.AllowedOperationsInterceptor.invoke(AllowedOperationsInterceptor.java:47)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
       at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:79)
       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.stateless.StatelessInstanceInterceptor.invoke(StatelessInstanceInterceptor.java:54)
       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.asynchronous.AsynchronousInterceptor.invoke(AsynchronousInterceptor.java:106)
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
       at org.jboss.ejb3.stateless.StatelessContainer.localInvoke(StatelessContainer.java:178)
       at org.jboss.ejb3.stateless.StatelessLocalProxy.invoke(StatelessLocalProxy.java:74)
       at $Proxy90.addUser(Unknown Source)
       at de.uni_mannheim.informatik.swt.struts.action.user.UserAddSaveAction.execute(UserAddSaveAction.java:27)
       at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:419)
       at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)
       at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
       at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
       at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
       at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
       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:868)
       at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:663)
       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: java.sql.BatchUpdateException: failed batch
       at org.hsqldb.jdbc.jdbcStatement.executeBatch(Unknown Source)
       at org.hsqldb.jdbc.jdbcPreparedStatement.executeBatch(Unknown Source)
       at org.jboss.resource.adapter.jdbc.CachedPreparedStatement.executeBatch(CachedPreparedStatement.java:478)
       at org.jboss.resource.adapter.jdbc.WrappedStatement.executeBatch(WrappedStatement.java:517)
       at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:58)
       at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:195)
       ... 57 more
      



      I figured out that this exception comes from EntityManager.flush() that is called somewhere in between my return statement from the SLSB.addUser() and the StrutsAction call of the SLSB.addUser().

       try {
       InitialContext ctx = new InitialContext();
       UserFacadeLocal facade = (UserFacadeLocal) ctx.lookup("urm/UserFacadeBean/local");
      
       String success = facade.addUser(username, password, email);
      
       } catch(Exception e) {
       }
      


       public String addUser(String username, String password, String email) {
       try {
       UserEntityBean userEntity = new UserEntityBean(username, password, email);
       em.persist(userEntity);
      // em.flush();
       return userEntity.getUsername();
       } catch(Exception e) {
       }
       }
      


      I am wondering

      "how to catch this exception? "

      I can insert a EntityManager.flush() in my code jsut before the return statement, but thean I still get this exception. I cannot catch it. Is there anything I missed?