I've got a small project ahead of me; there is an application that is not closing a connection, somewhere, somehow, it isn't clear where. We know it is happening because slowly the connections in the database will ramp up. I've set the max connections in tomcat so after 5 it will refuse more connections and it appears to be a situation where someone is actually closing the connection but not wrapping it in a try/catch because if I hammer a few select pages eventually I'll start to sneak in a few connections that refuse to close. The problem is that these pages do some serious work (not just crud), so lots of different database connections at different times.
What's the best way to debug a problem like this? Any special monitoring tools? Any special logging tools? I know it's a difficult problem because what is a valid unclosed connection?
I'm looking for a log, something like this:
That way I could simply see after running the app where a connection was opened and not closed. Anything like this? Any ideas/help would be appreciated
Joined: Dec 16, 2003
You might want to have a look at the jdbc driver you are using
and see whether it can be configured to do some tracing.
For example for Oracle have a look at what they say here.
"Eppur si muove!"
Joined: May 02, 2006
If we have access to the source code and can modify, please add close()/release() blocks everywhere you are create()/initiate() connection. Refactoring is the best place to fix this. I guess this will take less time than the debug and we have command also.
Joined: Jan 15, 2009
Ahh cool, I found a solution
So all database open calls went through a single java class: Database.getConnection() which returned a Connection object. I modified that method on my local machine to store that connection in a hashmap and put the "Stack" in a string as the key. That way I could loop through that hashmap and list all connections that were still open. I tossed that in a jsp file and hit the refresh button as I cruised through the app. Eventually I found connections in the hashmap that should have been closed but were not. It took awhile but I think I found all of them. Ahhh good times.