5 Replies Latest reply on Sep 16, 2014 9:47 AM by Scott Marlow

    Transaction problem with spring data jpa

    dmen Newbie

      Hi,

       

      while deploying on wildfly-8.1.0 we are getting the exception "Transaction is not active". We are trying to use spring-data-jpa repositories. The problem is when we are defining a method without a @Query annotation and call this method from an ejb service method with @TransactionAttribute(TransactionAttributeType.REQUIRED). In case the method is annotated with a custom query there is no problem.

       

      We have build a sample application demostrating the problem. When we deploy the same application on jboss AS 7.1.1 the application works without any problem (with or without the annotation @Query).

       

      Please find attached the sample project and the full stacktrace.

       

      Part of the stacktrace is provided below:

       

      Caused by: org.hibernate.exception.GenericJDBCException: Could not open connection

          at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:54)

          at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126)

          at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112)

          at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:235)

          at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.getConnection(LogicalConnectionImpl.java:171)

          at org.hibernate.engine.jdbc.internal.StatementPreparerImpl.connection(StatementPreparerImpl.java:63)

          at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$5.doPrepare(StatementPreparerImpl.java:162)

          at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:186)

          at org.hibernate.engine.jdbc.internal.StatementPreparerImpl.prepareQueryStatement(StatementPreparerImpl.java:160)

          at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1884)

          at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1861)

          at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1838)

          at org.hibernate.loader.Loader.doQuery(Loader.java:909)

          at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:354)

          at org.hibernate.loader.Loader.doList(Loader.java:2553)

          at org.hibernate.loader.Loader.doList(Loader.java:2539)

          at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2369)

          at org.hibernate.loader.Loader.list(Loader.java:2364)

          at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:496)

          at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:387)

          at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:231)

          at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1264)

          at org.hibernate.internal.QueryImpl.list(QueryImpl.java:103)

          at org.hibernate.jpa.internal.QueryImpl.list(QueryImpl.java:573)

          at org.hibernate.jpa.internal.QueryImpl.getSingleResult(QueryImpl.java:495)

          ... 113 more

      Caused by: java.sql.SQLException: javax.resource.ResourceException: IJ000460: Error checking for a transaction

          at org.jboss.jca.adapters.jdbc.WrapperDataSource.getConnection(WrapperDataSource.java:154)

          at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:139)

          at org.hibernate.internal.AbstractSessionImpl$NonContextualJdbcConnectionAccess.obtainConnection(AbstractSessionImpl.java:380)

          at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:228)

          ... 134 more

      Caused by: javax.resource.ResourceException: IJ000460: Error checking for a transaction

          at org.jboss.jca.core.connectionmanager.tx.TxConnectionManagerImpl.getManagedConnection(TxConnectionManagerImpl.java:416)

          at org.jboss.jca.core.connectionmanager.AbstractConnectionManager.allocateConnection(AbstractConnectionManager.java:513)

          at org.jboss.jca.adapters.jdbc.WrapperDataSource.getConnection(WrapperDataSource.java:146)

          ... 137 more

      Caused by: javax.resource.ResourceException: IJ000459: Transaction is not active: tx=TransactionImple < ac, BasicAction: 0:ffff0afa15d5:-3091a161:54117156:9 status: ActionStatus.ABORT_ONLY >

          at org.jboss.jca.core.connectionmanager.tx.TxConnectionManagerImpl.getManagedConnection(TxConnectionManagerImpl.java:400)

          ... 139 more

        • 1. Re: Transaction problem with spring data jpa
          Scott Marlow Master

          Looks like the transaction is marked to roll back only and IronJacamar is failing early (when getConnection is called to get a database connection).  The transaction is marked to roll back because of an earlier failure.  I didn't look at the attachments as I have seen this before.  If you don't want IronJacamar to fail early like this, I think you could try setting system property "ironjacamar.allow_marked_for_rollback" to true.  If your not sure how to set a system property, you could look at this example.

          • 2. Re: Transaction problem with spring data jpa
            dmen Newbie

            We set the system property 'ironjacamar.allow_marked_for_rollback' but the problem remains. Now we are getting an exception comes from arjuna transaction engine because it tries to register a SynchronizationImple which it is not feasible as the transaction status is 'ABORTED'.

             

            See the below stacktrace:

             

            Caused by: java.sql.SQLException: javax.resource.ResourceException: IJ000457: Unchecked throwable in managedConnectionReconnected() cl=org.jboss.jca.core.connectionmanager.listener.TxConnectionListener@11fade27[state=NORMAL managed connection=org.jboss.jca.adapters.jdbc.local.LocalManagedConnection@df9c940 connection handles=0 lastUse=1410518922292 trackByTx=false pool=org.jboss.jca.core.connectionmanager.pool.strategy.OnePool@4614f929 mcp=SemaphoreArrayListManagedConnectionPool@22a00fff[pool=ExampleDS] xaResource=LocalXAResourceImpl@24a9c59b[connectionListener=11fade27 connectionManager=608b0333 warned=false currentXid=null productName=H2 productVersion=1.3.173 (2013-07-28) jndiName=java:jboss/datasources/ExampleDS] txSync=null]
                at org.jboss.jca.adapters.jdbc.WrapperDataSource.getConnection(WrapperDataSource.java:154)
                at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:139)
                at org.hibernate.internal.AbstractSessionImpl$NonContextualJdbcConnectionAccess.obtainConnection(AbstractSessionImpl.java:380)
                at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:228)
                ... 134 more
            Caused by: javax.resource.ResourceException: IJ000457: Unchecked throwable in managedConnectionReconnected() cl=org.jboss.jca.core.connectionmanager.listener.TxConnectionListener@11fade27[state=NORMAL managed connection=org.jboss.jca.adapters.jdbc.local.LocalManagedConnection@df9c940 connection handles=0 lastUse=1410518922292 trackByTx=false pool=org.jboss.jca.core.connectionmanager.pool.strategy.OnePool@4614f929 mcp=SemaphoreArrayListManagedConnectionPool@22a00fff[pool=ExampleDS] xaResource=LocalXAResourceImpl@24a9c59b[connectionListener=11fade27 connectionManager=608b0333 warned=false currentXid=null productName=H2 productVersion=1.3.173 (2013-07-28) jndiName=java:jboss/datasources/ExampleDS] txSync=null]
                at org.jboss.jca.core.connectionmanager.AbstractConnectionManager.reconnectManagedConnection(AbstractConnectionManager.java:773)
                at org.jboss.jca.core.connectionmanager.AbstractConnectionManager.allocateConnection(AbstractConnectionManager.java:516)
                at org.jboss.jca.adapters.jdbc.WrapperDataSource.getConnection(WrapperDataSource.java:146)
                ... 137 more
            Caused by: javax.resource.ResourceException: IJ000461: Could not enlist in transaction on entering meta-aware object
                at org.jboss.jca.core.connectionmanager.tx.TxConnectionManagerImpl.managedConnectionReconnected(TxConnectionManagerImpl.java:546)
                at org.jboss.jca.core.connectionmanager.AbstractConnectionManager.reconnectManagedConnection(AbstractConnectionManager.java:768)
                ... 139 more
            Caused by: com.arjuna.ats.jta.exceptions.RollbackException: ARJUNA016081: The transaction implementation threw a RollbackException
                at com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionSynchronizationRegistryImple.registerInterposedSynchronization(TransactionSynchronizationRegistryImple.java:144)
                at org.jboss.jca.core.connectionmanager.transaction.TransactionSynchronizer.lock(TransactionSynchronizer.java:270)
                at org.jboss.jca.core.connectionmanager.listener.TxConnectionListener.enlist(TxConnectionListener.java:266)
                at org.jboss.jca.core.connectionmanager.tx.TxConnectionManagerImpl.managedConnectionReconnected(TxConnectionManagerImpl.java:539)
                ... 140 more
            Caused by: javax.transaction.RollbackException: ARJUNA016083: Can't register synchronization because the transaction is in aborted state
                at com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionImple.registerSynchronizationImple(TransactionImple.java:371)
                at com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionSynchronizationRegistryImple.registerInterposedSynchronization(TransactionSynchronizationRegistryImple.java:140)
                ... 143 more

            • 3. Re: Transaction problem with spring data jpa
              Scott Marlow Master

              You still need to figure out why the transaction is failing (something else is going wrong in the JTA transaction).  If you do not see enough information in the server.log, you can enable TRACE logging to get more info.

              1 of 1 people found this helpful
              • 4. Re: Transaction problem with spring data jpa
                dmen Newbie

                Thank you for the suggestion. It really helped us to further investigate this issue.

                 

                We have found, by enabling the trace logging, that the transaction is being rolled back when ever the entitymanager.createNamedQuery(...) is called with an invalid (i.e. non existing) query name as a parameter. Both wildfly 8.1.0 and JBoss 7.1.1 throw an IllegalArgumentException at that case, however wildfly will also change the transaction status to rollback (is this the correct behavior? take notice that the exception is being handled and not re-thrown). See the sample code bellow:

                 

                     @TransactionAttribute(TransactionAttributeType.REQUIRED)
                     public void find() {
                          try {
                               log.debug("Before call to entityManager.createNamedQuery(...)");
                               entityManager.createNamedQuery("NonExistentQueryName");
                          }
                          catch (IllegalArgumentException e) {
                               // at this point wildfly will have set the transaction to rollback
                               log.debug("Catch exception ", e.getMessage());
                          }
                          finally {
                               log.debug("After call to entityManager.createNamedQuery(...)");
                          }
                     }
                

                 

                We have attached the output from jboss 7 and wildfly 8, as well as the updated version of the code.

                 

                For the actual spring-data code that triggers the issue see here: NamedQuery.java , specifically the method lookupFrom(JpaQueryMethod method, EntityManager em) at line 106 and constructor NamedQuery(JpaQueryMethod method, EntityManager em) at line 62.

                • 5. Re: Re: Transaction problem with spring data jpa
                  Scott Marlow Master

                  dmen wrote:

                   

                  Thank you for the suggestion. It really helped us to further investigate this issue.

                   

                  We have found, by enabling the trace logging, that the transaction is being rolled back when ever the entitymanager.createNamedQuery(...) is called with an invalid (i.e. non existing) query name as a parameter. Both wildfly 8.1.0 and JBoss 7.1.1 throw an IllegalArgumentException at that case, however wildfly will also change the transaction status to rollback (is this the correct behavior? take notice that the exception is being handled and not re-thrown). See the sample code bellow:

                   

                  WildFly is more correct.  From the JPA 2.1 spec:

                  3.1.1EntityManager Interface

                  ....  javadoc and some text omitted

                   

                  Runtime exceptions thrown by the methods of the EntityManager interface other than the LockTimeoutException will cause the current transaction to be marked for rollback if the persistence context is joined to that transaction.

                   

                  So, the transaction is marked for "rollback only" within your call to entityManager.createNamedQuery("NonExistentQueryName").  Catching the exception doesn't help since this happens in the entityManager call.