This topic has actually been
discussed before and a solution is posted as a
FAQ. However, this solution does not provide a reasoning behind it, so I thought I would follow up just to clarify.
We have been experiencing a StaleConnectionException for some time, and we narrowed it down to WebSphere's pooling as the culprit (surprise - kind of like when we discovered that itn always gives you a Connection with autocommit set to
true). Today, I read this in their Connection Pool documentation (WebSphere 4, but this
probably applies to WS 5, too):
Code:
DISABLE_AUTO_CONN_CLEANUP - Specifies whether or not the connection is closed at the end of a transaction. The default is false, which indicates that when a transaction is completed, WebSphere connection pooling closes the connection and all associated resources and returns the connection to the pool. This means that any use of the connection after the transaction has ended results in a StaleConnectionException, because the connection has been closed and returned to the pool. This mechanism ensures that connections are not held indefinitely by the application. If the value is set to true, the connection is not returned to the pool at the end of a transaction. In this case, the application must return the connection to the pool by explicitly calling close(). If the application does not close the connection, the pool will run out of connections for other applications to use. This is different from orphaning connections, because connections in a transaction cannot be orphaned.
I'm not sure why this is the default behavior. For our web application, we are using one ThreadLocal Session (Spring's OpenSessionInView helper class) for an entire web request. For some requests, multiple database transaction occur. Since we have "Disable Auto Cleanup" set to false, it appears that when our first transaction commits, WebSphere effectively closes the Connection and returns it to the pool. Yuck!
Anyway, thought I would try to clear things up for anybody wondering why checking this option makes the problem go away.
Ryan