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

前端 未结 3 1886
梦毁少年i
梦毁少年i 2021-01-13 04:41

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.

相关标签:
3条回答
  • 2021-01-13 05:00

    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.

    0 讨论(0)
  • 2021-01-13 05:08

    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.

    0 讨论(0)
  • 2021-01-13 05:09

    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

    0 讨论(0)
提交回复
热议问题