Home > Hibernate > Transaction is not active: tx=TransactionImple < ac, BasicAction

Transaction is not active: tx=TransactionImple < ac, BasicAction

This is an exception I was getting while trying to do some hibernate stuff.

Caused by: org.hibernate.exception.GenericJDBCException: Cannot open connection
    at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:126)
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:114)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:52)
    at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:449)
    at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:167)
    at org.hibernate.jdbc.AbstractBatcher.prepareQueryStatement(AbstractBatcher.java:161)
    at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1573)
    at org.hibernate.loader.Loader.doQuery(Loader.java:696)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
    at org.hibernate.loader.Loader.doList(Loader.java:2228)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2125)
    at org.hibernate.loader.Loader.list(Loader.java:2120)
    at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:401)
    at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:361)
    at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
    at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1148)
    at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
    at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:67)
    ... 97 more
Caused by: org.jboss.util.NestedSQLException: Transaction is not active: tx=TransactionImple < ac, BasicAction: -53effdb8:7d1:4bb313e3:7a status: ActionStatus.ABORT_ONLY >; - nested throwable: (javax.resource.ResourceException: Transaction is not active: tx=TransactionImple < ac, BasicAction: -53effdb8:7d1:4bb313e3:7a status: ActionStatus.ABORT_ONLY >)
    at org.jboss.resource.adapter.jdbc.WrapperDataSource.getConnection(WrapperDataSource.java:95)
    at org.hibernate.ejb.connection.InjectedDataSourceConnectionProvider.getConnection(InjectedDataSourceConnectionProvider.java:46)
    at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:446)
    ... 111 more
Caused by: javax.resource.ResourceException: Transaction is not active: tx=TransactionImple < ac, BasicAction: -53effdb8:7d1:4bb313e3:7a status: ActionStatus.ABORT_ONLY >
    at org.jboss.resource.connectionmanager.TxConnectionManager.getManagedConnection(TxConnectionManager.java:370)
    at org.jboss.resource.connectionmanager.BaseConnectionManager2.allocateConnection(BaseConnectionManager2.java:496)
    at org.jboss.resource.connectionmanager.BaseConnectionManager2$ConnectionManagerProxy.allocateConnection(BaseConnectionManager2.java:941)
    at org.jboss.resource.adapter.jdbc.WrapperDataSource.getConnection(WrapperDataSource.java:89)
    ... 113 more

It turned out that this is not a hibernate or jboss exception at all. The actual cause of this was a NullPointerException somewhere in the flow of the application. This caused the transaction to end and re-enter a loop when the transaction was not active and this was thrown. So if you ever get this exception I suggest you put some break points in your code and see where it fails. It’s likely that there is an unchecked exception thrown somewhere in your programme.

Categories: Hibernate Tags: ,
  1. Yury
    7 April 2010 at 7:00 am

    Thank you very much, mate. You really saved my time!

    • 7 April 2010 at 7:49 am

      Hello Yury, thank you for your nice words. It took me a while to figure it out as well so I thought I’d post it here.

      • 20 August 2019 at 3:40 pm

        Nearly a decade later, this post is still out there really helping, truly saved my bacon. 🙂 Thanks so much!

  2. 14 June 2010 at 9:14 am

    nice article, thanks very much,,

  3. Bhat
    17 June 2010 at 12:20 pm

    Hey Panos,

    I am facing similar issues with my app.
    But The unchecked exception which i get is not consistent.

    another thing ..
    can u please elaborate on your statement – ‘ This caused the transaction to end and re-enter a loop when the transaction was not active and this was thrown’.

    Thnx
    Bhat

  4. 17 June 2010 at 2:45 pm

    Hello Bhat, in my case there was a for loop. When the transaction occurred, the flow of the application did not exit the for loop, but instead re-entered it, and the exception happened. So instead of seeing the actual reason I was seeing the “transaction is not active” which was caused by another exception (a NullPointerException in my case). What I suggest is what I also said in my post. Put a break point in your application and see where it fails. Then go and fix the failing statement because the “transactoin is not active” error probably means that something has previously gone wrong (like a npe or an ArrayIndexOutofBounds) which caused the transaction scope to end.

  5. 29 July 2010 at 12:56 am

    I spent so much time thinking this was a transaction error. Thanks for your tip that it was something else. It did turn out to be an NPE!

  6. John Goyer
    28 January 2011 at 5:18 pm

    This tip saved me a lot of time. In my case the exception that was causing the problem was in fact an intentionally produced one. I am using embedded JBoss with
    junit, hibernate and jta transactions. In one test I insert a value in a hibernate
    entity that violates an FK constraint. The test is labeled with the annotation:

    @Test( expected=javax.persistence.EntityExistsException.class )

    which is the exception I get when I do this:

    tm.begin();
    em.merge( p1 );
    tm.commit();

    Thus junit was swallowing the exception, reporting that everything was fine — it was the NEXT test case that failed, of course.

    Solution was easy once I understood the problem:

    try {
    tm.begin();
    em.merge( p1 );
    tm.commit();
    }
    finally {
    tm.rollback();
    }

  7. 18 July 2011 at 11:49 am

    This is slightly odd. Is there no proper way to get the Exception Stacktrace of the “real” problem? (the NullPointerException?) Without debug-stepping through the code, I mean?

    • 18 July 2011 at 1:04 pm

      Only if you surround the offensive code with a try… catch block of code.

  8. snk
    28 September 2011 at 6:19 pm

    This saved me a lot of time. Thank you very much.

  9. Sham Phadtale
    15 October 2011 at 5:11 pm

    I checked but I did not got any exception as u said 😦 In my case above exception is coming when q.getSingleResult(); line gets executed. Do you have any idea why it is happen? This exception does not come always, it comes only when no of records to be merged to database are more than 4/5. I am iterating over records and at a time merging one record to MySql database/

    • 16 October 2011 at 9:41 am

      What happens if you wrap the q.getSingleResult(); in a try.. catch block and catch the exception? Maybe the q.getSingleResult(); returns more than one result and it throws an exception.

  10. kdawg
    2 December 2011 at 3:10 pm

    Just in case someone stumbles by on this thread. getSingleResult(), if it FAILS to return any result will throw an exception. EVEN IF this exception is caught and handled correctly, the Tx is silently invalidated and any future DB requests will fail. Not the smartest behavior, but there it is.

  11. Khoa
    23 December 2011 at 9:31 am

    thanx a lot. this saved my life 🙂

  12. Carlo
    5 January 2012 at 12:02 pm

    We have this problem since we changed our hibernate configuration switching from a JDBCTransactionFactory to a JTATransactionFactory.
    Using the JDBCTransactionFactory everything work fine with the JTATransactionFactory in a specific workflow we have such trace error.

    Any idea Guys?

  13. 5 January 2012 at 12:22 pm

    Carlos can you check if another exception occurs? Try putting a try… catch block around the offensive code. Probably the JTA manager does something different from the JDBC manager.

  14. suchit gupta
    23 March 2012 at 9:13 am

    Thanks A lot .

    In case also the problem was a null pointer exception .

  15. Yunshi
    11 May 2012 at 9:19 am

    Thanks A lot . It saves much time to see your article.

  16. Katerina
    13 September 2012 at 12:41 pm

    Thanks a lot Panos!!!You saved my time and helped me figure out the problem 2-days before the delivery! In my case what caused the transaction to end was another PersistenceException. Thanks again! Great article!

  17. Mike
    10 October 2012 at 11:17 pm

    Thanks, Panos – your post saved me an enormous amount of time and frustration. The note by kdawg on getSingleResult() was also very helpful, as we were relying on continued access to the transaction after swallowing a throw from this method.

    Another source of trouble may be worth noting. I ran into a situation in which an entity of type A references and entity of type B (a one-to-one relationship / cascade all), and the entity of type B references a set of entities of type C (a many:many relationship with no cascading). I attempted to persist an instance of entity type A that referenced an unpersisted instance of entity type B, which in turn referenced an single instance of an entity of type C, where this latter instance (of C) was NOT persisted. Hibernate couldn’t persist the instance of B because doing so would violate the foreign key constraint with C, but no exception was thrown from the entity manager, and nothing showed up in my postgres log. However, the transaction was borked (put in ABORT_ONLY status) and failed upon commit. No amount of stepping though my code (without stepping through Hibernate’s code) would expose this problem. This on Hibernate 3.6.6, JBoss 6.1. So, if you step through your code and still don’t find a problem, you might look at your cascading relationships.

  18. hmedsb@bluewin.ch
    16 January 2013 at 10:43 am

    Thanks for saving my time too!!

  19. 4 February 2013 at 5:46 am

    I was checking for the exception. This post has made me check deep on Tx.
    Thanks a lot !!!

  20. Leandro Ramírez
    25 June 2013 at 9:13 pm

    Thanks you!!! / Muchas gracias!!!

  21. Jeffre
    21 August 2013 at 5:47 pm

    Thanks for the help. It was a getSingleResult in my case.

  22. Christian Viana
    13 December 2019 at 8:51 pm

    Thank you! Your post helped me a lot today. I was stuck in a problem I thought it was hibernate second level cache related, but at the end of the day it was the problem you described: unchecked exception putting the transaction in an inconsistent state.
    Thank you again!!!

  1. 31 August 2012 at 12:39 am

Leave a reply to Christian Viana Cancel reply