We've found a bug in old code where connections aren't being closed. It's an easy fix, but I'm wondering how we go about proving that it's fixed. There is a choice of using a connection pool or not. For the pooling use it would be easy to add monitoring for the pool, but when connections pooling is not used, how do we track those unclosed, orphaned connections? Is it just like any other memory leak?
The bug looks like basically a cut and paste error. We have a few classes that manage the DB connection, so it looks roughly like this:
OurDBConn conn1 = ConnectionManager.getConnection();
try {
// business logic
} catch () {
//
} finally {
ConnectionManager.returnConnection(conn1);
}
/// and then later in the same method
OurDBConn conn2 = ConnectionManager.getConnection();
try {
// business logic
} catch () {
//
} finally {
ConnectionManager.returnConnection(conn1); // NOTE Error: conn1 should be conn2
}
I don't know why the earlier coders didn't just reuse the original connection, but that's what it is
(begin edit/append)
Yes, the connection code is ours as well and so I can use the answers given.
However, I don't think I asked the right question, although the answers below answer the question I asked. I'm not sure what the right stackoverflow thing to do is; ask another question, or edit this one?
One of the question I should have asked is: how would these orphaned, un-closed connections manifest themselves in system performance? Also, since these connection objects exist only within the scope of a certain method, wouldn't the connections be eligible for garbage collection? And then if they are gc'ed, what is the effect of open connections being gc'ed?
(end edit)