连接与托管connection.org.jboss.resource.adapter.jdbc.jdk6.WrappedConnectionJDK6无关

时间:2011-12-07 09:14:53

标签: hibernate jboss5.x

我在JBoss服务器上使用Hibernate。我收到以下错误。

当我尝试在同一个sesssion中第二次连接到数据库时发生错误。

此外,我收到错误"关闭连接。请关闭您的连接"。

    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)

可能是什么原因,我该如何解决这种情况?

3 个答案:

答案 0 :(得分:8)

自从你一年前提出这个问题以来,这个答案实际上可能已经太迟了。但它将帮助那些将来会遇到这个错误的人。

您的错误可能来自不同的来源,但在我的情况下,所有关于事务超时,一些查询可能需要很长时间才能达到超时,并且hibernate会抛出异常。在我的情况下,我所做的是将事务超时设置为更高的值。这解决了我的问题。

这是一个有用的链接。 The transaction is not active!

Understanding JDBC internal timeouts config

-cheers

答案 1 :(得分:1)

我在事务超时完全不相关的设置中也遇到了这个问题。

具体来说,我的代码中有以下错误:

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();

...正如您所看到的,即使SQL字符串不包含?占位符,代码也会尝试设置参数。这引发了一个错误,显然将连接置于破坏/不可恢复的状态。因此,在我的异常处理代码尝试commit与普通conn.commit()的连接时,我会得到以下跟踪,这与您的非常相似,即使与超时完全无关:

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)

不可否认,异常处理代码应该尝试改为rollback连接,而不是commit - 但这与此问题无关,并且,对于它的价值,您仍会看到完全相同的例外,虽然跟踪略有不同:

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]

底线是,当您尝试对由于先前的SQLException而进入某种错误/不稳定状态的连接执行某些操作时,也可能抛出此异常。它不一定是超时。

答案 2 :(得分:0)

关闭连接时会发生这种情况,之后再次使用该连接。

想象一下这样一种场景:对于bean调用,在开始时创建连接,并在最后关闭连接。对于数据库上的各种CRUD操作,操作本身使用连接。在操作过程中的某处,连接关闭然后再次使用。

在伪代码中:

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

现在假设doOperations中某处你做了类似的事情:

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
        // ...
    }
}

抛出的异常是:

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)

底线:不要关闭Connection,然后再次使用它。