Yesterday I was investigating a “Connection is not associated with a managed connection” error that occurred in our application. Browsing through the log files, I discovered that we accidentally leaked a connection when retrieving data from the database failed.
The following happened:
A ClassCastException occurred while Hibernate was resolving a ManyToOne relationship. One of the classes involved was not Serializable. Because the error handling in our application did not close the session, JBoss cleaned up for us and the CachedConnectionManager reported: Closing a connection for you. Please close them yourself.
Obviously, this means there are two errors in the application. One of the classes needed to be Serializable, and our error handling wasn’t closing the session properly.
However, straight after that another error was shown in the logging:
javax.servlet.ServletException: Error invoking cached connection manager
Caused by: javax.resource.ResourceException: Some connections were not closed, see the log for the allocation stacktraces
... 6 more
This confuses me a little. JBoss detects that we left a connection open (we have the properties ‘debug’ and ‘error’ both set to true in the jca-jboss-beans.xml), but it seems that when JBoss tries to close it, something else goes wrong. Why closing fails, I do not know. The ResourceException says “see the log file for allocation stacktraces”, but I’m not sure what to look for?
After all of the above, the Hibernate Session object thinks its session object and connection are still in good shape. The session.isOpen() and the session.isConnected() both return true. However, the underlying JDBC connection isn’t there anymore. Any attempt to use the Session object afterwards results in the “Connection is not associated with a managed connection” error mentioned above.
We are using JBoss 5 and Hibernate 3.2. We plan to upgrade Hibernate to a new version, but haven’t been able to find the time. So I'm stuck for now with this version.
I realize our application contains a serious error by not closing the session, and that that is the area whe I need to apply a fix. But I’m trying to understand how it’s possible that closing the connection fails and how it’s possible that Hibernate still thinks everything is ok, while the underlying JDBC connection has been closed/destroyed. Shouldn't JBoss signal that the connection close failed and inform Hibernate?
Any help is appreciated.