5 Replies Latest reply on May 9, 2017 9:31 AM by Anup Dey

    Hibernate Exceptions not propagated to Application deployed in JBoss 6.4

    Manikandan Padmanabhan Newbie

      Issue: The Web application uses JPA/Hibernate to communicate with H2 database. This application is deployed in JBoss EAP 6.4 and uses its Hibernate modules. When an 'org.hibernate.exception.ConstraintViolationException' is thrown from hibernate, it is not caught in the exception block of the calling method from DAO. Due to this the service layer is not able to wrap the exception and propagate it to the UI layer. Any help on this issue is really appreciated.

       

      Here is the configuratrion.

      1) persistence.xml

       

      persistence-unit name="ecmFunctionalRouteWeb" transaction-type="JTA">

      <provider>org.hibernate.ejb.HibernatePersistence</provider>

      <class></class>

      <jta-data-source>java:/ecmConfigDS</jta-data-source>

      <properties>

      <!-- <property name="javax.persistence.jdbc.driver" value="org.h2.Driver" />

       

      <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />

      <property name="hibernate.max_fetch_depth" value="3" />

      <property name="hibernate.hbm2ddl.auto" value="validate" />

      <property name="hibernate.show_sql" value="true" />

      <property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.JBossAppServerJtaPlatform"/>

      </properties>

      </persistence-unit>

      2) Spring dispatcher-servlet.xml

       

      <bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">

      <property name="database" value="H2" />

      <property name="showSql" value="true" />

      <property name="generateDdl" value="true" />

      </bean>

       

      <jee:jndi-lookup id="dataSource" jndi-name="java:/ecmConfigDS" />

       

      <bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">

      <property name="jtaDataSource" ref="dataSource" />

      </bean>


      <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

      <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager" />

      <tx:annotation-driven transaction-manager="transactionManager"/>

       

      3) Exception stacktrace from the server.log

      javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not execute statement
      at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1387) [hibernate-entitymanager-4.2.22.Final-redhat-1.jar:4.2.22.Final-redhat-1]
      at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1310) [hibernate-entitymanager-4.2.22.Final-redhat-1.jar:4.2.22.Final-redhat-1]
      at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1316) [hibernate-entitymanager-4.2.22.Final-redhat-1.jar:4.2.22.Final-redhat-1]
      at org.hibernate.ejb.AbstractEntityManagerImpl$CallbackExceptionMapperImpl.mapManagedFlushFailure(AbstractEntityManagerImpl.java:1510) [hibernate-entitymanager-4.2.22.Final-redhat-1.jar:4.2.22.Final-redhat-1]
      at org.hibernate.engine.transaction.synchronization.internal.SynchronizationCallbackCoordinatorNonTrackingImpl.beforeCompletion(SynchronizationCallbackCoordinatorNonTrackingImpl.java:114) [hibernate-core-4.2.22.Final-redhat-1.jar:4.2.22.Final-redhat-1]
      at org.hibernate.engine.transaction.synchronization.internal.RegisteredSynchronization.beforeCompletion(RegisteredSynchronization.java:53) [hibernate-core-4.2.22.Final-redhat-1.jar:4.2.22.Final-redhat-1]
      at com.arjuna.ats.internal.jta.resources.arjunacore.SynchronizationImple.beforeCompletion(SynchronizationImple.java:76)
      at com.arjuna.ats.arjuna.coordinator.TwoPhaseCoordinator.beforeCompletion(TwoPhaseCoordinator.java:273)
      at com.arjuna.ats.arjuna.coordinator.TwoPhaseCoordinator.end(TwoPhaseCoordinator.java:93)
      at com.arjuna.ats.arjuna.AtomicAction.commit(AtomicAction.java:162)
      at com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionImple.commitAndDisassociate(TransactionImple.java:1189)
      at com.arjuna.ats.internal.jta.transaction.arjunacore.BaseTransaction.commit(BaseTransaction.java:126)
      at com.arjuna.ats.jbossatx.BaseTransactionManagerDelegate.commit(BaseTransactionManagerDelegate.java:75)
      at org.jboss.tm.usertx.client.ServerVMClientUserTransaction.commit(ServerVMClientUserTransaction.java:173)
      at org.springframework.transaction.jta.JtaTransactionManager.doCommit(JtaTransactionManager.java:1021) [spring-tx-4.0.2.RELEASE.jar:4.0.2.RELEASE]
      at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:757) [spring-tx-4.0.2.RELEASE.jar:4.0.2.RELEASE]
      at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:726) [spring-tx-4.0.2.RELEASE.jar:4.0.2.RELEASE]
      at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:478) [spring-tx-4.0.2.RELEASE.jar:4.0.2.RELEASE]
      at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:272) [spring-tx-4.0.2.RELEASE.jar:4.0.2.RELEASE]
      at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95) [spring-tx-4.0.2.RELEASE.jar:4.0.2.RELEASE]
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) [spring-aop-4.0.2.RELEASE.jar:4.0.2.RELEASE]
      at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207) [spring-aop-4.0.2.RELEASE.jar:4.0.2.RELEASE]
      at com.sun.proxy.$Proxy537.deleteFunctionalRoute(Unknown Source)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.8.0_121]
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) [rt.jar:1.8.0_121]
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [rt.jar:1.8.0_121]
      at java.lang.reflect.Method.invoke(Method.java:498) [rt.jar:1.8.0_121]
      at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215) [spring-web-4.0.2.RELEASE.jar:4.0.2.RELEASE]
      at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132) [spring-web-4.0.2.RELEASE.jar:4.0.2.RELEASE]
      at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104) [spring-webmvc-4.0.2.RELEASE.jar:4.0.2.RELEASE]
      at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749) [spring-webmvc-4.0.2.RELEASE.jar:4.0.2.RELEASE]
      at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:690) [spring-webmvc-4.0.2.RELEASE.jar:4.0.2.RELEASE]
      at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83) [spring-webmvc-4.0.2.RELEASE.jar:4.0.2.RELEASE]
      at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:945) [spring-webmvc-4.0.2.RELEASE.jar:4.0.2.RELEASE]
      at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876) [spring-webmvc-4.0.2.RELEASE.jar:4.0.2.RELEASE]
      at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961) [spring-webmvc-4.0.2.RELEASE.jar:4.0.2.RELEASE]
      at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852) [spring-webmvc-4.0.2.RELEASE.jar:4.0.2.RELEASE]
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:734) [jboss-servlet-api_3.0_spec-1.0.2.Final-redhat-2.jar:1.0.2.Final-redhat-2]
      at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837) [spring-webmvc-4.0.2.RELEASE.jar:4.0.2.RELEASE]
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.2.Final-redhat-2.jar:1.0.2.Final-redhat-2]
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:295) [jbossweb-7.5.15.Final-redhat-1.jar:7.5.15.Final-redhat-1]
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214) [jbossweb-7.5.15.Final-redhat-1.jar:7.5.15.Final-redhat-1]
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:231) [jbossweb-7.5.15.Final-redhat-1.jar:7.5.15.Final-redhat-1]
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:149) [jbossweb-7.5.15.Final-redhat-1.jar:7.5.15.Final-redhat-1]
      at org.jboss.as.jpa.interceptor.WebNonTxEmCloserValve.invoke(WebNonTxEmCloserValve.java:50) [jboss-as-jpa-7.5.7.Final-redhat-3.jar:7.5.7.Final-redhat-3]
      at org.jboss.as.jpa.interceptor.WebNonTxEmCloserValve.invoke(WebNonTxEmCloserValve.java:50) [jboss-as-jpa-7.5.7.Final-redhat-3.jar:7.5.7.Final-redhat-3]
      at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:169) [jboss-as-web-7.5.7.Final-redhat-3.jar:7.5.7.Final-redhat-3]
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:150) [jbossweb-7.5.15.Final-redhat-1.jar:7.5.15.Final-redhat-1]
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:97) [jbossweb-7.5.15.Final-redhat-1.jar:7.5.15.Final-redhat-1]
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:102) [jbossweb-7.5.15.Final-redhat-1.jar:7.5.15.Final-redhat-1]
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:344) [jbossweb-7.5.15.Final-redhat-1.jar:7.5.15.Final-redhat-1]
      at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:854) [jbossweb-7.5.15.Final-redhat-1.jar:7.5.15.Final-redhat-1]
      at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:654) [jbossweb-7.5.15.Final-redhat-1.jar:7.5.15.Final-redhat-1]
      at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:926) [jbossweb-7.5.15.Final-redhat-1.jar:7.5.15.Final-redhat-1]
      at java.lang.Thread.run(Thread.java:745) [rt.jar:1.8.0_121]
      Caused by: org.hibernate.exception.ConstraintViolationException: could not execute statement
      at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:129) [hibernate-core-4.2.22.Final-redhat-1.jar:4.2.22.Final-redhat-1]
      at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49) [hibernate-core-4.2.22.Final-redhat-1.jar:4.2.22.Final-redhat-1]
      at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:124) [hibernate-core-4.2.22.Final-redhat-1.jar:4.2.22.Final-redhat-1]
      at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:109) [hibernate-core-4.2.22.Final-redhat-1.jar:4.2.22.Final-redhat-1]
      at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:189) [hibernate-core-4.2.22.Final-redhat-1.jar:4.2.22.Final-redhat-1]
      at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:59) [hibernate-core-4.2.22.Final-redhat-1.jar:4.2.22.Final-redhat-1]
      at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:3355) [hibernate-core-4.2.22.Final-redhat-1.jar:4.2.22.Final-redhat-1]
      at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:3558) [hibernate-core-4.2.22.Final-redhat-1.jar:4.2.22.Final-redhat-1]
      at org.hibernate.action.internal.EntityDeleteAction.execute(EntityDeleteAction.java:102) [hibernate-core-4.2.22.Final-redhat-1.jar:4.2.22.Final-redhat-1]
      at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:395) [hibernate-core-4.2.22.Final-redhat-1.jar:4.2.22.Final-redhat-1]
      at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:387) [hibernate-core-4.2.22.Final-redhat-1.jar:4.2.22.Final-redhat-1]
      at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:310) [hibernate-core-4.2.22.Final-redhat-1.jar:4.2.22.Final-redhat-1]
      at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:349) [hibernate-core-4.2.22.Final-redhat-1.jar:4.2.22.Final-redhat-1]
      at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:56) [hibernate-core-4.2.22.Final-redhat-1.jar:4.2.22.Final-redhat-1]
      at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1195) [hibernate-core-4.2.22.Final-redhat-1.jar:4.2.22.Final-redhat-1]
      at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:404) [hibernate-core-4.2.22.Final-redhat-1.jar:4.2.22.Final-redhat-1]
      at org.hibernate.engine.transaction.synchronization.internal.SynchronizationCallbackCoordinatorNonTrackingImpl.beforeCompletion(SynchronizationCallbackCoordinatorNonTrackingImpl.java:109) [hibernate-core-4.2.22.Final-redhat-1.jar:4.2.22.Final-redhat-1]
      ... 51 more
      Caused by: org.h2.jdbc.JdbcSQLException: Referential integrity constraint violation: "FK_879T3FJWO2XJUHP5QQ349TPKI: PUBLIC.ALLOWED_MIME_TYPE FOREIGN KEY(FRID) REFERENCES PUBLIC.FUNCTIONAL_ROUTE(FRID) ('DAM_BusLicense')"; SQL statement:
      delete from FUNCTIONAL_ROUTE where FRID=? [23503-193]
      at org.h2.message.DbException.getJdbcSQLException(DbException.java:345)
      at org.h2.message.DbException.get(DbException.java:179)
      at org.h2.message.DbException.get(DbException.java:155)
      at org.h2.constraint.ConstraintReferential.checkRow(ConstraintReferential.java:426)
      at org.h2.constraint.ConstraintReferential.checkRowRefTable(ConstraintReferential.java:443)
      at org.h2.constraint.ConstraintReferential.checkRow(ConstraintReferential.java:318)
      at org.h2.table.Table.fireConstraints(Table.java:967)
      at org.h2.table.Table.fireAfterRow(Table.java:985)
      at org.h2.command.dml.Delete.update(Delete.java:101)
      at org.h2.command.CommandContainer.update(CommandContainer.java:98)
      at org.h2.command.Command.executeUpdate(Command.java:258)
      at org.h2.server.TcpServerThread.process(TcpServerThread.java:344)
      at org.h2.server.TcpServerThread.run(TcpServerThread.java:158)
      at java.lang.Thread.run(Thread.java:745)

      at org.h2.engine.SessionRemote.done(SessionRemote.java:624) [h2-1.4.190.jar:1.4.190]
      at org.h2.command.CommandRemote.executeUpdate(CommandRemote.java:191) [h2-1.4.190.jar:1.4.190]
      at org.h2.jdbc.JdbcPreparedStatement.executeUpdateInternal(JdbcPreparedStatement.java:160) [h2-1.4.190.jar:1.4.190]
      at org.h2.jdbc.JdbcPreparedStatement.executeUpdate(JdbcPreparedStatement.java:146) [h2-1.4.190.jar:1.4.190]
      at org.jboss.jca.adapters.jdbc.WrappedPreparedStatement.executeUpdate(WrappedPreparedStatement.java:493)
      at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:186) [hibernate-core-4.2.22.Final-redhat-1.jar:4.2.22.Final-redhat-1]
      ... 63 more

       

       

       

        • 1. Re: Hibernate Exceptions not propagated to Application deployed in JBoss 6.4
          Anup Dey Apprentice

          One of your table has some constraint (e.g unique/FK) which is preventing the  delete the query to be executed. Check your table constraints and modify your code.

           

          - Check all the association mapping is properly done or not?

          - Check the Mapping relationship is working properly inside hibernate Entity classes as well as database side?

           

          It is failing while executing the delete statement (the foreign key constraints) . Check the FK constraints in the below SQL statement:

           

          ~~~

          org.h2.jdbc.JdbcSQLException: Referential integrity constraint violation: "FK_879T3FJWO2XJUHP5QQ349TPKI: PUBLIC.ALLOWED_MIME_TYPE FOREIGN KEY(FRID) REFERENCES PUBLIC.FUNCTIONAL_ROUTE(FRID) ('DAM_BusLicense')"; SQL statement:

          delete from FUNCTIONAL_ROUTE where FRID=?

          ~~~

           

          Note: Hibernate code was written with the expectation that the DB supports cascading deletes, which H2 does not.

           

          • 2. Re: Hibernate Exceptions not propagated to Application deployed in JBoss 6.4
            Manikandan Padmanabhan Newbie

            Thanks Anup for the reply.I understand the error and perhaps I should have provide more clarity to the issue.The SQLException is obvious and I wanted to have this exception relayed to service layer. So far that reason, I have the catch block in DAO with super exception which will catch and throw back custom exception to service layer.try{    dao.update(entity);}catch(Throwable t){}Unfortunately the SQLException is not caught in this catch block and so could not throw the custom exception.Yes, I am using H2 database. Is it due to this that H2 drivers does not have the capability to throw the exception. Or is there some configurations that I need to define in hibernate properties.I really appreciate your help.

            • 3. Re: Hibernate Exceptions not propagated to Application deployed in JBoss 6.4
              Viral Gohel Newbie

              In your case, spring instantiates an Entity Manager Factory and you have the Spring's JTA Transaction Manager also set. Not sure how you execute the SQL queries, but if you are using Spring's JdbcTemplate or named queries, then the exception that you are getting is a checked exception, and this not is not thrown by the any of the JdbcTemplate.query(...) methods.

               

              Spring translates this to one of the DataAccessException which is more generic family of runtime exceptions, in order to abstract away any specific underlying database implementation.

              • 4. Re: Hibernate Exceptions not propagated to Application deployed in JBoss 6.4
                Manikandan Padmanabhan Newbie

                I am using JPA 2.1 API i.e    javax.persistence.criteria.CriteriaBuilder to build the query and execute it. And not using Spring JDBC template or Namedqueries for this purpose.I am using Jboss Hibernate system as JPA vendor and H2 database for this purpose. I guess there should be some properties in the hibernate for JBoss specific or some configurations missing in the Jboss standalone that prevents propagating this exception to the application.

                Thanks for taking your time and replying to this question.

                 

                 

                 

                CriteriaBuilder

                • 5. Re: Hibernate Exceptions not propagated to Application deployed in JBoss 6.4
                  Anup Dey Apprentice

                  I would say simply try the delete query without using hibernate Criteria query and check whether its is working or not?

                  - Try the same query in other DB vendor as well like MySQl, Oracle etc..

                  - Try to run the SQL native query in the DB client and check whether the query is properly working or not?

                  -  Check the dialect Class "org.hibernate.dialect.H2Dialect"  is present inside the driver what you're using?

                  - It says clearly constraint violation exception... Check  is there mismatch between Mapping and DataBase. May be your database is not using case sensitive.

                  - Might be you need check with other DB, See the doc below:

                   

                  Chapter 2. Setup and configuration