File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes Finding connections that aren't closing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Finding connections that aren Watch "Finding connections that aren New topic

Finding connections that aren't closing

Caden Whitaker

Joined: Jan 15, 2009
Posts: 24
Hey all,

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
Gian Franco
Ranch Hand

Joined: Dec 16, 2003
Posts: 977
Hi Caden,

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!"
giridhar challa

Joined: May 02, 2006
Posts: 18
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.
Caden Whitaker

Joined: Jan 15, 2009
Posts: 24
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.
I agree. Here's the link:
subject: Finding connections that aren't closing
It's not a secret anymore!