Connection is not associated with a managed connection.org.jboss.resource.adapter.jdbc.jdk6.WrappedConnectionJDK6

走远了吗. 提交于 2019-12-19 09:02:19

问题


I am using Hibernate on a JBoss server. I get the error below.

The error happens when I try to connect to the database for the second time in the same sesssion.

Also I get the error "Closing connection for you. Please close your connection".

    14:28:37,869 ERROR [HibernateUtil] HibernateException  occurred in executeQuery  method   in  HibernateUtil class 
    org.hibernate.exception.GenericJDBCException: could not execute query
    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.loader.Loader.doList(Loader.java:2231)
    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 com.a.amc.dao.utils.HibernateUtil.executeQuery(HibernateUtil.java:154)
    at com.a.amc.service.impl.CityServiceImpl.isCityExists(CityServiceImpl.java:142)
    at com.a.amc.service.impl.CityServiceImpl.addCity(CityServiceImpl.java:38)
    at com.a.amc.web.actions.CityAction.addCity(CityAction.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)

14:28:37,869 ERROR [JDBCTransaction] Could not toggle autocommit
java.sql.SQLException: Connection is not associated with a managed connection.org.jboss.resource.adapter.jdbc.jdk6.WrappedConnectionJDK6@1269ca1
    at org.jboss.resource.adapter.jdbc.WrappedConnection.lock(WrappedConnection.java:81)
    at org.jboss.resource.adapter.jdbc.WrappedConnection.setAutoCommit(WrappedConnection.java:454)
    at org.hibernate.transaction.JDBCTransaction.toggleAutoCommit(JDBCTransaction.java:228)
    at org.hibernate.transaction.JDBCTransaction.rollbackAndResetAutoCommit(JDBCTransaction.java:220)
    at org.hibernate.transaction.JDBCTransaction.rollback(JDBCTransaction.java:196)
    at com.a.amc.dao.utils.HibernateUtil.executeQuery(HibernateUtil.java:159)
    at com.a.amc.service.impl.CityServiceImpl.isCityExists(CityServiceImpl.java:142)
    at com.a.amc.service.impl.CityServiceImpl.addCity(CityServiceImpl.java:38)
    at com.a.amc.web.actions.CityAction.addCity(CityAction.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)

14:28:37,869 ERROR [JDBCTransaction] JDBC rollback failed
java.sql.SQLException: Connection is not associated with a managed connection.org.jboss.resource.adapter.jdbc.jdk6.WrappedConnectionJDK6@1269ca1
    at org.jboss.resource.adapter.jdbc.WrappedConnection.lock(WrappedConnection.java:81)
    at org.jboss.resource.adapter.jdbc.WrappedConnection.rollback(WrappedConnection.java:496)
    at org.hibernate.transaction.JDBCTransaction.rollbackAndResetAutoCommit(JDBCTransaction.java:217)
    at org.hibernate.transaction.JDBCTransaction.rollback(JDBCTransaction.java:196)
    at com.a.amc.dao.utils.HibernateUtil.executeQuery(HibernateUtil.java:159)
    at com.a.amc.service.impl.CityServiceImpl.isCityExists(CityServiceImpl.java:142)
    at com.a.amc.service.impl.CityServiceImpl.addCity(CityServiceImpl.java:38)
    at com.a.amc.web.actions.CityAction.addCity(CityAction.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)

14:28:37,869 ERROR [CityServiceImpl] Exception  occurred in isCityExists method in  CityServiceImpl
org.hibernate.TransactionException: JDBC rollback failed
    at org.hibernate.transaction.JDBCTransaction.rollback(JDBCTransaction.java:204)
    at com.a.amc.dao.utils.HibernateUtil.executeQuery(HibernateUtil.java:159)
    at com.a.amc.service.impl.CityServiceImpl.isCityExists(CityServiceImpl.java:142)
    at com.a.amc.service.impl.CityServiceImpl.addCity(CityServiceImpl.java:38)
    at com.a.amc.web.actions.CityAction.addCity(CityAction.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)

What could be the cause, and how do I solve this situation?


回答1:


This answer may actually too late since you asked this a year ago. but it will help those who will encounter this error in the future.

Your error may came from different source, but in my case its all about the transaction timeout, some of the query may take long so the timeout is reach and hibernate throws an exception. In my case what i did was set the transaction timeout to much higher value. Which solves my problem.

Here is a useful link. The transaction is not active!

Understanding JDBC internal timeouts config

-cheers




回答2:


I've also encountered this problem in a setting entirely unrelated to the transaction timeout.

Specifically, I had the following bug in my code:

String SQL = "SELECT * FROM someTable WHERE id=1"; // no ? placholders !!
ps = conn.prepareStatement(SQL);
ps.setInt(1, id); // dude, there's no parameter #1
rs = ps.executeQuery();

… as you can see the code tries to set a parameter even though the SQL String contains no ? placeholders. That threw an error and apparently placed the connection in a busted / unsalvageable state. As such, at the point where my exception handling code was trying to commit the connection with a plain conn.commit() I would get the following trace which is very similar to yours even though entirely unrelated to timeouts:

Caused by: java.sql.SQLException: Connection is not associated with a managed connection.org.jboss.jca.adapters.jdbc.jdk6.WrappedConnectionJDK6@17309c54
at org.jboss.jca.adapters.jdbc.WrappedConnection.lock(WrappedConnection.java:155)
at org.jboss.jca.adapters.jdbc.WrappedConnection.commit(WrappedConnection.java:750)

Admittedly, the exception handling code should have tried to instead rollback the connection, instead of commit-ing it but this is unrelated to this issue and, for what it's worth, you would still see the exact same exception albeit with a slightly different trace:

Caused by: java.sql.SQLException: Connection is not associated with a managed connection.org.jboss.jca.adapters.jdbc.jdk6.WrappedConnectionJDK6@7303676e
at org.jboss.jca.adapters.jdbc.WrappedConnection.lock(WrappedConnection.java:155)
at org.jboss.jca.adapters.jdbc.WrappedConnection.rollback(WrappedConnection.java:771)
at org.apache.commons.dbutils.DbUtils.rollback(DbUtils.java:297) [commons-dbutils-1.6.jar:1.6]

Bottom line is that this exception maybe also be thrown when you try to do something with a connection that has entered some kind of erroneous / unstable state as a result of a previous SQLException. It doesn't have to be a timeout.




回答3:


This can happen when closing a connection, and afterwards using that connection again.

Imagine a scenario where for a bean call, at the start the connection is created and at the very end the connection is closed. The operation itself uses the connection, for all sorts of CRUD operations on a database. Somewhere during the operation the connection is closed and then used again.

In pseudo code:

try( Connection c = createConnection(); ) { // the very start
    doOperations( c );
}

Now suppose somewhere in doOperations you do something like:

void doOperations( Connection c ) {
    String sql = ""; // an actual SQL statement
    try( Connection c2 = c;
         PreparedStatement ps = c2.prepareStatement( sql ); ) {
        // ...
        ps.executeUpdate( );
    } // <<< the connection will be closed here

    String sql2 = ""; // an actual SQL statement
    try( PreparedStatement ps2 = c.prepareStatement( sql2 ); ) { // <<< exception thrown here, connection is already closed
        // ...
    }
}

The exception thrown is:

java.sql.SQLException: Connection is not associated with a managed connection.org.jboss.jca.adapters.jdbc.jdk6.WrappedConnectionJDK6@2cc0951e
at org.jboss.jca.adapters.jdbc.WrappedConnection.lock(WrappedConnection.java:154)
at org.jboss.jca.adapters.jdbc.WrappedConnection.prepareStatement(WrappedConnection.java:394)

Bottom line: don't close a Connection and then use it again.



来源:https://stackoverflow.com/questions/8412828/connection-is-not-associated-with-a-managed-connection-org-jboss-resource-adapte

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!