9 Replies Latest reply on Dec 25, 2009 8:49 PM by alex becker

    Transaction Rollback - Catching a ConstraintViolationException

    Matthias Hüller Newbie

      Hi,


      I have an Entity class with an @UniqueConstraint Annotation over several fields:


      
      @Entity
      
      @Table(name = "PR_KONTEN_STANDARDHZINS",
      
          uniqueConstraints = {@UniqueConstraint(columnNames = {"KONDITIONSGRUPPE", "GUELTIGAB"})}
      
      )
      
      public class AccountStandardHInterest extends AbstractEntity {
      
      .
      
      .
      
      .
      
      }
      
      



      Now I try to save a new row using the createSave() method.


      
      @Stateful
      
      @Scope(ScopeType.CONVERSATION)
      
      @Name("de.xxx.xxxxxxx.pricing.helptable.AccountStandardHInterestBean")
      
      public class AccountStandardHInterestBeanAction implements AccountStandardHInterestBean {
      
      
          @In
      
          private EntityManager entityManager;
      
      
          @DataModelSelection
      
          @In(required = false)
      
          @Out(required = false)
      
          private AccountStandardHInterest accountStandardHInterest;
      
          .
      
          .
      
          .
      
          @SuppressWarnings("unchecked")
      
          @Begin(nested = true, flushMode = FlushModeType.MANUAL)
      
          public PageNode init() {
      
              .
      
              .
      
              .
      
              return PricingPageNodes.HT_ACCOUNTSTANDARDHINTEREST_LIST;
      
          }
      
          .
      
          .
      
          .
      
          public PageNode createSave() {
      
              entityManager.persist(accountStandardHInterest);
      
              entityManager.flush();
      
              return init();
      
          }
      
          .
      
          .
      
          .
      
      }
      
      



      When I violate the above specified unique constraint I get the following stacktrace and the JBoss Seam debug page is shown:


      
      11:13:46,814 WARN  [JDBCExceptionReporter] SQL Error: 1, SQLState: 23000
      
      11:13:46,814 ERROR [JDBCExceptionReporter] ORA-00001: unique constraint (FORCE2.SYS_C00148331) violated
      
      
      11:13:46,814 WARN  [JDBCExceptionReporter] SQL Error: 1, SQLState: 23000
      
      11:13:46,814 ERROR [JDBCExceptionReporter] ORA-00001: unique constraint (FORCE2.SYS_C00148331) violated
      
      
      11:13:46,814 ERROR [AbstractFlushingEventListener] Could not synchronize database state with session
      
      org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
      
           at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
      
           at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
      
           at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:253)
      
           at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266)
      
           at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:167)
      
           at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
      
           at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
      
           at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
      
           at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:297)
      
           at org.jboss.seam.persistence.EntityManagerProxy.flush(EntityManagerProxy.java:90)
      
           at de.xxx.xxxxxxxx.pricing.helptable.AccountStandardHInterestBeanAction.createSave(AccountStandardHInterestBeanAction.java:104)
      
           at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      
           at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
      
           at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
      
           at java.lang.reflect.Method.invoke(Unknown Source)
      
           at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:112)
      
           at org.jboss.ejb3.interceptor.InvocationContextImpl.proceed(InvocationContextImpl.java:166)
      
           at org.jboss.seam.intercept.EJBInvocationContext.proceed(EJBInvocationContext.java:44)
      
           at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:56)
      
           at org.jboss.seam.core.BijectionInterceptor.aroundInvoke(BijectionInterceptor.java:46)
      
           at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
      
           at org.jboss.seam.persistence.ManagedEntityIdentityInterceptor.aroundInvoke(ManagedEntityIdentityInterceptor.java:48)
      
           at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
      
           at org.jboss.seam.transaction.RollbackInterceptor.aroundInvoke(RollbackInterceptor.java:31)
      
           at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
      
           at org.jboss.seam.core.ConversationInterceptor.aroundInvoke(ConversationInterceptor.java:65)
      
           at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
      
           at org.jboss.seam.core.MethodContextInterceptor.aroundInvoke(MethodContextInterceptor.java:42)
      
           at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
      
           at org.jboss.seam.persistence.EntityManagerProxyInterceptor.aroundInvoke(EntityManagerProxyInterceptor.java:26)
      
           at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
      
           at org.jboss.seam.persistence.HibernateSessionProxyInterceptor.aroundInvoke(HibernateSessionProxyInterceptor.java:27)
      
           at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
      
           at org.jboss.seam.intercept.RootInterceptor.invoke(RootInterceptor.java:107)
      
           at org.jboss.seam.intercept.SessionBeanInterceptor.aroundInvoke(SessionBeanInterceptor.java:50)
      
           at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      
           at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
      
           at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
      
           at java.lang.reflect.Method.invoke(Unknown Source)
      
           at org.jboss.ejb3.interceptor.InvocationContextImpl.proceed(InvocationContextImpl.java:118)
      
           at org.jboss.ejb3.interceptor.EJB3InterceptorsInterceptor.invoke(EJB3InterceptorsInterceptor.java:63)
      
           at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
      
           at org.jboss.ejb3.entity.ExtendedPersistenceContextPropagationInterceptor.invoke(ExtendedPersistenceContextPropagationInterceptor.java:57)
      
           at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
      
           at org.jboss.ejb3.entity.TransactionScopedEntityManagerInterceptor.invoke(TransactionScopedEntityManagerInterceptor.java:54)
      
           at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
      
           at org.jboss.ejb3.AllowedOperationsInterceptor.invoke(AllowedOperationsInterceptor.java:47)
      
           at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
      
           at org.jboss.aspects.tx.TxPolicy.invokeInCallerTx(TxPolicy.java:126)
      
           at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:195)
      
           at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
      
           at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:95)
      
           at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
      
           at org.jboss.ejb3.stateful.StatefulInstanceInterceptor.invoke(StatefulInstanceInterceptor.java:83)
      
           at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
      
           at org.jboss.aspects.security.AuthenticationInterceptor.invoke(AuthenticationInterceptor.java:77)
      
           at org.jboss.ejb3.security.Ejb3AuthenticationInterceptor.invoke(Ejb3AuthenticationInterceptor.java:110)
      
           at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
      
           at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:46)
      
           at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
      
           at org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(AsynchronousInterceptor.java:106)
      
           at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
      
           at org.jboss.ejb3.stateful.StatefulContainer.localInvoke(StatefulContainer.java:206)
      
           at org.jboss.ejb3.stateful.StatefulLocalProxy.invoke(StatefulLocalProxy.java:119)
      
           at $Proxy407.createSave(Unknown Source)
      
           at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      
           at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
      
           at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
      
           at java.lang.reflect.Method.invoke(Unknown Source)
      
           at org.jboss.seam.util.Reflections.invoke(Reflections.java:21)
      
           at org.jboss.seam.intercept.RootInvocationContext.proceed(RootInvocationContext.java:31)
      
           at org.jboss.seam.intercept.ClientSideInterceptor$1.proceed(ClientSideInterceptor.java:76)
      
           at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:56)
      
           at org.jboss.seam.ejb.RemoveInterceptor.aroundInvoke(RemoveInterceptor.java:41)
      
           at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
      
           at org.jboss.seam.intercept.RootInterceptor.invoke(RootInterceptor.java:107)
      
           at org.jboss.seam.intercept.ClientSideInterceptor.invoke(ClientSideInterceptor.java:54)
      
           at org.javassist.tmp.java.lang.Object_$$_javassist_6.createSave(Object_$$_javassist_6.java)
      
           at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      
           at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
      
           at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
      
           at java.lang.reflect.Method.invoke(Unknown Source)
      
           at org.jboss.el.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:329)
      
           at org.jboss.el.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:342)
      
           at org.jboss.el.parser.AstPropertySuffix.invoke(AstPropertySuffix.java:58)
      
           at org.jboss.el.parser.AstValue.invoke(AstValue.java:96)
      
           at org.jboss.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276)
      
           at com.sun.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:68)
      
           at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
      
           at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
      
           at javax.faces.component.UICommand.broadcast(UICommand.java:387)
      
           at org.ajax4jsf.component.AjaxViewRoot.processEvents(AjaxViewRoot.java:184)
      
           at org.ajax4jsf.component.AjaxViewRoot.broadcastEvents(AjaxViewRoot.java:162)
      
           at org.ajax4jsf.component.AjaxViewRoot.processApplication(AjaxViewRoot.java:350)
      
           at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:82)
      
           at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:100)
      
           at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
      
           at javax.faces.webapp.FacesServlet.service(FacesServlet.java:265)
      
           at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
      
           at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
      
           at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:83)
      
           at org.jboss.seam.web.MultipartFilter.doFilter(MultipartFilter.java:85)
      
           at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
      
           at org.jboss.seam.web.ExceptionFilter.doFilter(ExceptionFilter.java:64)
      
           at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
      
           at org.jboss.seam.web.RedirectFilter.doFilter(RedirectFilter.java:45)
      
           at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
      
           at org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:141)
      
           at org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:281)
      
           at org.jboss.seam.web.Ajax4jsfFilter.doFilter(Ajax4jsfFilter.java:60)
      
           at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
      
           at org.jboss.seam.web.LoggingFilter.doFilter(LoggingFilter.java:58)
      
           at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
      
           at org.jboss.seam.debug.hot.HotDeployFilter.doFilter(HotDeployFilter.java:68)
      
           at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
      
           at org.jboss.seam.servlet.SeamFilter.doFilter(SeamFilter.java:158)
      
           at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
      
           at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
      
           at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
      
           at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
      
           at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
      
           at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
      
           at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
      
           at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:179)
      
           at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:432)
      
           at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
      
           at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
      
           at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
      
           at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157)
      
           at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
      
           at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:262)
      
           at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
      
           at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
      
           at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:446)
      
           at java.lang.Thread.run(Unknown Source)
      
      Caused by: java.sql.BatchUpdateException: ORA-00001: unique constraint (FORCE2.SYS_C00148331) violated
      
      
           at oracle.jdbc.driver.DatabaseError.throwBatchUpdateException(DatabaseError.java:343)
      
           at oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:10720)
      
           at org.jboss.resource.adapter.jdbc.WrappedStatement.executeBatch(WrappedStatement.java:519)
      
           at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
      
           at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:246)
      
           ... 132 more
      
      11:13:46,830 FATAL [application] javax.ejb.EJBTransactionRolledbackException: org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
      
      javax.faces.el.EvaluationException: javax.ejb.EJBTransactionRolledbackException: org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
      
           at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
      
           at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
      
           at javax.faces.component.UICommand.broadcast(UICommand.java:387)
      
           at org.ajax4jsf.component.AjaxViewRoot.processEvents(AjaxViewRoot.java:184)
      
           at org.ajax4jsf.component.AjaxViewRoot.broadcastEvents(AjaxViewRoot.java:162)
      
           at org.ajax4jsf.component.AjaxViewRoot.processApplication(AjaxViewRoot.java:350)
      
           at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:82)
      
           at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:100)
      
           at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
      
           at javax.faces.webapp.FacesServlet.service(FacesServlet.java:265)
      
           at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
      
           at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
      
           at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:83)
      
           at org.jboss.seam.web.MultipartFilter.doFilter(MultipartFilter.java:85)
      
           at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
      
           at org.jboss.seam.web.ExceptionFilter.doFilter(ExceptionFilter.java:64)
      
           at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
      
           at org.jboss.seam.web.RedirectFilter.doFilter(RedirectFilter.java:45)
      
           at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
      
           at org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:141)
      
           at org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:281)
      
           at org.jboss.seam.web.Ajax4jsfFilter.doFilter(Ajax4jsfFilter.java:60)
      
           at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
      
           at org.jboss.seam.web.LoggingFilter.doFilter(LoggingFilter.java:58)
      
           at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
      
           at org.jboss.seam.debug.hot.HotDeployFilter.doFilter(HotDeployFilter.java:68)
      
           at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
      
           at org.jboss.seam.servlet.SeamFilter.doFilter(SeamFilter.java:158)
      
           at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
      
           at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
      
           at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
      
           at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
      
           at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
      
           at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
      
           at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
      
           at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:179)
      
           at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:432)
      
           at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
      
           at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
      
           at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
      
           at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157)
      
           at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
      
           at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:262)
      
           at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
      
           at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
      
           at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:446)
      
           at java.lang.Thread.run(Unknown Source)
      
      Caused by: javax.ejb.EJBTransactionRolledbackException: org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
      
           at org.jboss.ejb3.tx.Ejb3TxPolicy.handleInCallerTx(Ejb3TxPolicy.java:87)
      
           at org.jboss.aspects.tx.TxPolicy.invokeInCallerTx(TxPolicy.java:130)
      
           at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:195)
      
           at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
      
           at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:95)
      
           at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
      
           at org.jboss.ejb3.stateful.StatefulInstanceInterceptor.invoke(StatefulInstanceInterceptor.java:83)
      
           at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
      
           at org.jboss.aspects.security.AuthenticationInterceptor.invoke(AuthenticationInterceptor.java:77)
      
           at org.jboss.ejb3.security.Ejb3AuthenticationInterceptor.invoke(Ejb3AuthenticationInterceptor.java:110)
      
           at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
      
           at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:46)
      
           at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
      
           at org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(AsynchronousInterceptor.java:106)
      
           at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
      
           at org.jboss.ejb3.stateful.StatefulContainer.localInvoke(StatefulContainer.java:206)
      
           at org.jboss.ejb3.stateful.StatefulLocalProxy.invoke(StatefulLocalProxy.java:119)
      
           at $Proxy407.createSave(Unknown Source)
      
           at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      
           at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
      
           at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
      
           at java.lang.reflect.Method.invoke(Unknown Source)
      
           at org.jboss.seam.util.Reflections.invoke(Reflections.java:21)
      
           at org.jboss.seam.intercept.RootInvocationContext.proceed(RootInvocationContext.java:31)
      
           at org.jboss.seam.intercept.ClientSideInterceptor$1.proceed(ClientSideInterceptor.java:76)
      
           at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:56)
      
           at org.jboss.seam.ejb.RemoveInterceptor.aroundInvoke(RemoveInterceptor.java:41)
      
           at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
      
           at org.jboss.seam.intercept.RootInterceptor.invoke(RootInterceptor.java:107)
      
           at org.jboss.seam.intercept.ClientSideInterceptor.invoke(ClientSideInterceptor.java:54)
      
           at org.javassist.tmp.java.lang.Object_$$_javassist_6.createSave(Object_$$_javassist_6.java)
      
           at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      
           at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
      
           at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
      
           at java.lang.reflect.Method.invoke(Unknown Source)
      
           at org.jboss.el.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:329)
      
           at org.jboss.el.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:342)
      
           at org.jboss.el.parser.AstPropertySuffix.invoke(AstPropertySuffix.java:58)
      
           at org.jboss.el.parser.AstValue.invoke(AstValue.java:96)
      
           at org.jboss.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276)
      
           at com.sun.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:68)
      
           at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
      
           ... 46 more
      
      Caused by: javax.persistence.EntityExistsException: org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
      
           at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:605)
      
           at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:300)
      
           at org.jboss.seam.persistence.EntityManagerProxy.flush(EntityManagerProxy.java:90)
      
           at de.xxx.xxxxxxxx.pricing.helptable.AccountStandardHInterestBeanAction.createSave(AccountStandardHInterestBeanAction.java:104)
      
           at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      
           at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
      
           at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
      
           at java.lang.reflect.Method.invoke(Unknown Source)
      
           at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:112)
      
           at org.jboss.ejb3.interceptor.InvocationContextImpl.proceed(InvocationContextImpl.java:166)
      
           at org.jboss.seam.intercept.EJBInvocationContext.proceed(EJBInvocationContext.java:44)
      
           at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:56)
      
           at org.jboss.seam.core.BijectionInterceptor.aroundInvoke(BijectionInterceptor.java:46)
      
           at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
      
           at org.jboss.seam.persistence.ManagedEntityIdentityInterceptor.aroundInvoke(ManagedEntityIdentityInterceptor.java:48)
      
           at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
      
           at org.jboss.seam.transaction.RollbackInterceptor.aroundInvoke(RollbackInterceptor.java:31)
      
           at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
      
           at org.jboss.seam.core.ConversationInterceptor.aroundInvoke(ConversationInterceptor.java:65)
      
           at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
      
           at org.jboss.seam.core.MethodContextInterceptor.aroundInvoke(MethodContextInterceptor.java:42)
      
           at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
      
           at org.jboss.seam.persistence.EntityManagerProxyInterceptor.aroundInvoke(EntityManagerProxyInterceptor.java:26)
      
           at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
      
           at org.jboss.seam.persistence.HibernateSessionProxyInterceptor.aroundInvoke(HibernateSessionProxyInterceptor.java:27)
      
           at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
      
           at org.jboss.seam.intercept.RootInterceptor.invoke(RootInterceptor.java:107)
      
           at org.jboss.seam.intercept.SessionBeanInterceptor.aroundInvoke(SessionBeanInterceptor.java:50)
      
           at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      
           at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
      
           at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
      
           at java.lang.reflect.Method.invoke(Unknown Source)
      
           at org.jboss.ejb3.interceptor.InvocationContextImpl.proceed(InvocationContextImpl.java:118)
      
           at org.jboss.ejb3.interceptor.EJB3InterceptorsInterceptor.invoke(EJB3InterceptorsInterceptor.java:63)
      
           at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
      
           at org.jboss.ejb3.entity.ExtendedPersistenceContextPropagationInterceptor.invoke(ExtendedPersistenceContextPropagationInterceptor.java:57)
      
           at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
      
           at org.jboss.ejb3.entity.TransactionScopedEntityManagerInterceptor.invoke(TransactionScopedEntityManagerInterceptor.java:54)
      
           at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
      
           at org.jboss.ejb3.AllowedOperationsInterceptor.invoke(AllowedOperationsInterceptor.java:47)
      
           at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
      
           at org.jboss.aspects.tx.TxPolicy.invokeInCallerTx(TxPolicy.java:126)
      
           ... 86 more
      
      Caused by: org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
      
           at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
      
           at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
      
           at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:253)
      
           at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266)
      
           at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:167)
      
           at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
      
           at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
      
           at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
      
           at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:297)
      
           ... 126 more
      
      Caused by: java.sql.BatchUpdateException: ORA-00001: unique constraint (FORCE2.SYS_C00148331) violated
      
      
           at oracle.jdbc.driver.DatabaseError.throwBatchUpdateException(DatabaseError.java:343)
      
           at oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:10720)
      
           at org.jboss.resource.adapter.jdbc.WrappedStatement.executeBatch(WrappedStatement.java:519)
      
           at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
      
           at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:246)
      
           ... 132 more
      
      



      Is there a way to rollback the transaction? Or do I have to catch the ConstraintViolationException. A try{} catch(ConstraintViolationException e) {} im my createSave method doesn't solve the problem.


      Thanks a lot!!


      altes-kind

        • 1. Re: Transaction Rollback - Catching a ConstraintViolationException
          Tom Goring Apprentice

          I solved this problem using a Interceptor.  Wrote my own Interceptor that catched that exception, added faces msg and returned null (no navigation).

          • 2. Re: Transaction Rollback - Catching a ConstraintViolationException
            Matthias Hüller Newbie

            Hi Tom,


            thanks for you answer. Using a Interceptor... quite a interesting solution. :-)


            Could you please add some code to show me how you implemented this Interceptor?


            Thanks a lot!!


            altes-kind

            • 3. Re: Transaction Rollback - Catching a ConstraintViolationException
              Tom Goring Apprentice


              @Target(ElementType.TYPE)
              @Retention(RetentionPolicy.RUNTIME)
              @Interceptors(FuiPojoInterceptor.class)
              public @interface PojoInterceptor {
              
              }





              @Name("fuiPojoInterceptor")
              @Interceptor(stateless=true, within=BijectionInterceptor.class)
              public class FuiPojoInterceptor {
              
                   private Log log = Logging.getLog(FuiPojoInterceptor.class);
              
              
                   @AroundInvoke
                   public Object intercept(InvocationContext ctx) throws Exception {
                        try {
                             return ctx.proceed();
                        } catch (EJBTransactionRolledbackException ex) {
              
                             log.info("Intercepting Exception #0 on object #1 ", ex
                                       .getMessage(), ctx.getTarget());
              
              
                             DomainEventLog domainEventLog = (DomainEventLog)Component.getInstance(DomainEventLog.Name);
                             domainEventLog.processRollback();
                             
                             // TODO Resource Bundle ...
                             FacesMessages.instance().addFromResourceBundleOrDefault(
                                       FacesMessage.SEVERITY_ERROR, "jnet", ex.getMessage());
                             return null;
                        } 
                   }
                   
              



              Then add the annotation to your POJO / SFSB @PojoInterceptor

              • 4. Re: Transaction Rollback - Catching a ConstraintViolationException
                Benjamin Graf Novice

                Hi Tom,


                I have an additional question to this solution. If using seam managed transactions with fliushmodetype=manual. The raw interceptor usuage seems to eliminate the global transaktion. After correcting values (go back to page with interceptor) and resubmiting nothing is commited to the database.


                Thx for help!

                • 5. Re: Transaction Rollback - Catching a ConstraintViolationException
                  Sylvain Leroux Newbie

                  Hi,


                  I spend most of my time yesterday working on a similar problem. Here are not one but two other solutions!





                  First, you may simply require a new trasanction for the method that may trigger the constraint violation exception. You do that by using the @TransactionAttribute annotation. AFAIK, this require a container managed transaction (the default EJB session bean behavior):


                  @Stateful
                  @Scope(CONVERSATION)
                  @Name("creation_moule")
                  @TransactionManagement(TransactionManagementType.CONTAINER)
                  public class CreationMoule implements CreationMoulePubli {
                      @Logger
                      Log logger;
                  
                      @PersistenceContext(type=PersistenceContextType.EXTENDED)
                      private EntityManager em;
                  
                  
                      @Override
                      @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
                      public void essayerEnregistrerMoule() {
                       try {
                           em.persist(moule);
                           em.flush();
                       } 
                       catch (javax.persistence.EntityExistsException e) {
                           logger.error("catched EntityExistsException " + e.toString());
                       }
                       catch(Exception e) {
                           logger.error("catched generic Exception " + e.getMessage());
                       }
                       logger.error("end");
                      }
                  }
                  





                  Second, you may use bean managed trasanction (BMT) - @TransactionManagement(TransactionManagementType.BEAN). In that case, you replace your container regarding transaction management for a given bean.


                  I encapsulate this in an utility class called BMPHelper since I have to use it on several session bean:


                  /**
                   * Bean Managed Persistence Helper class.
                   * @author Sylvain Leroux
                   */
                  public class BMPHelper {
                      private EntityManagerFactory emf;
                  
                      public BMPHelper(EntityManagerFactory emf) {
                       this.emf = emf;
                      }
                  
                      public void persist(Object obj) throws NamingException,
                           NotSupportedException, SystemException {
                       UserTransaction userTransaction = (UserTransaction) new InitialContext()
                            .lookup("java:comp/UserTransaction");
                       userTransaction.begin();
                       EntityManager em = emf.createEntityManager();
                       try {
                           em.persist(obj);
                           em.flush();
                       } catch (javax.persistence.EntityExistsException e) {
                           userTransaction.rollback();
                           throw e;
                       } finally {
                           em.close();
                       }
                      }
                  }
                  



                  And you use it like that:


                  @Stateful
                  @Scope(CONVERSATION)
                  @Name("creation_moule")
                  @TransactionManagement(TransactionManagementType.BEAN)
                  public class CreationMoule implements CreationMoulePubli {
                      @Logger
                      Log logger;
                  
                      @PersistenceUnit
                      private EntityManagerFactory emf;
                  
                  
                      @Override
                      public void essayerEnregistrerMoule() {
                          bmpHelper = new BMPHelper(emf);
                       try {
                           bmpHelper.persist(moule);
                       } 
                       catch (javax.persistence.EntityExistsException e) {
                           logger.error("catched EntityExistsException " + e.toString());
                       }
                       catch(Exception e) {
                           logger.error("catched generic Exception " + e.getMessage());
                       }
                       logger.error("end");
                      }
                  }
                  






                  Just a word of warning: I'm new to transaction management so, I cannot tell which of this solution is the best, nor I can't guarantee this code to be bullet-proof! Use at your own risks ;)



                  Hope this will help,

                  Sylvain

                  • 6. Re: Transaction Rollback - Catching a ConstraintViolationException
                    Tom Goring Apprentice

                    Hi,


                    I've just tested this and it works with my own set up (which is a little different to yours).


                    my Pojo action handler has the interceptor to trap the error and add the message.  The pojo action handler starts the conv with flushMode=FlushModeType.MANUAL.  the pojo calls a SLSB (my business layer) to add the record.  The SLSB throws the constraint problem and the error is presented to the user.  After fixing the error the user is able to add the record (in same conv).


                    My SLSB layer always calls flush at the end of the operation (as we are using manual flush mode).


                    Hope this helps ?


                    N.B. I had do do a few tricks with the auto allocated key and version as EJB3 does not set them back to what they were before the operation failed.... and thus you get invalid optimistic locking exceptions


                    • 7. Re: Transaction Rollback - Catching a ConstraintViolationException
                      Pete Muir Master

                      Normally I would suggest not catching the exception, but instead prevalidating your data by checking if you will validate the constraint and not executing the persist() if it does. Once the exception has occurred, the transaction needs to be restarted to be used.

                      • 8. Re: Transaction Rollback - Catching a ConstraintViolationException
                        Matthias Hüller Newbie

                        Sorry for my late response.
                        How can I restart a transaction? Could you please post some sample code and give me a broader description about it?


                        public PageNode createSave() {
                        
                        
                         try {
                        
                          entityManager.persist(accountGroup);
                        
                          entityManager.flush();
                        
                         } catch (Exception e) {
                        
                          // Something went wrong - need to restart the transaction
                        
                          return null;
                        
                         }
                        
                        
                         return init();
                        
                        }



                        Thanks!!!

                        • 9. Re: Transaction Rollback - Catching a ConstraintViolationException
                          alex becker Newbie

                          @Pete


                          Hi Pete, i have the same problem (an Exception was thrown and i lost my active transaktion during the rollback).
                          Could you tell us, how can we restart the transaktion?
                          What about my entities? They where loaded inside the fist transaktion and detached now.


                          thanks.
                          alex