Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Check JDBC connection leaks

 
Dan Parsons
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am always checking if any of my JDBC has Database leaks with our Oracle 9i database.

Here is what I use in SQL Plus:


If I dont close my connections and do alot of JDBC inserts and updates in my Web Application, the query will show many lines of JDBC Thin Client showing up.
When I do close my connections correctly and do alot of inserts and updates in my Web Application, the above query doesnt show any programs running so I assume I dont have any database leaks?

This is my way and would like to know how experienced Java developers check to make sure all their Database connections are closed?
[ October 23, 2007: Message edited by: Dan Parsons ]
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34681
367
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Dan Parsons:
would like to know how experienced Java developers check to make sure all their Database connections are closed?

For code we write, we check by only having one class that knows about connections. If this is the superclass, we can do all the work (creating sql statement, setting parameters and processing results) in the subclass. The superclass is known to be good through code review. Everything else is known to be good as it doesn't instantiate connections; just implement methods.

For code we inherit, we use profilers to see if there are leaks along with doing code reviews.
 
Paul Campbell
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wouldn't get to used to that method... most Oracle configurations (in this day of SOx requirements and heightened security) do not allow discretionary users or applications select privileges on v$session. It will likely catch up to your location as well.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I made a little thing that never exposes connections. It has logic like:

Those closes are in a finally block so it's impossible to forget to close things. It also eliminates a lot of duplicate setup and exception handling. There are similar update and insert methods that return the number of rows affected.

I understand Spring has a very nice JDBC abstraction layer that does something like this. I was not able to introduce any open source at this time so I didn't pursue it.

Would that kind of design work for you?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic