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.
Thank you very much, mate. You really saved my time!
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.
Nearly a decade later, this post is still out there really helping, truly saved my bacon. 🙂 Thanks so much!
nice article, thanks very much,,
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
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.
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!
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();
}
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?
Only if you surround the offensive code with a try… catch block of code.
This saved me a lot of time. Thank you very much.
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/
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.
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.
thanx a lot. this saved my life 🙂
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?
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.
Thanks A lot .
In case also the problem was a null pointer exception .
Thanks A lot . It saves much time to see your article.
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!
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.
Thanks for saving my time too!!
I was checking for the exception. This post has made me check deep on Tx.
Thanks a lot !!!
Thanks you!!! / Muchas gracias!!!
Thanks for the help. It was a getSingleResult in my case.
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!!!