This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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
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.