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 DB Connection object tracking 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 "DB Connection object tracking" Watch "DB Connection object tracking" New topic

DB Connection object tracking

Raga Baskaran

Joined: Sep 16, 2010
Posts: 3

How can a DB connection object created by DataSource.getConnection() be tracked.
i.e., How to make sure the receiver of the connection is closing it properly or not.

Let's assume i'm exposing a method that returns the connection object(created by datasource lookup followed by datasource.getConnection()) to the caller which the caller can use to perform some DB operations.

If the above method is used in multiple places, how can one make sure that the callers are closing the connection or not ?
Since DataSource is a managed object , Can we count/track the connection leaks ?

Rob Spoor

Joined: Oct 27, 2005
Posts: 20276

Moving to JDBC.

How To Ask Questions How To Answer Questions
David Newton

Joined: Sep 29, 2008
Posts: 12617

Off the top of my head, unless there was a timeout for holding on to a connection, I don't see how-how would you distinguish between a process that had a connection, legitimately, versus a process that just forgot to close the connection? Perhaps if you tracked the entries and exits of those processes, with filters/AOP advice/etc, butnthen why not just delegate the opening and closing of connections to those?

I guess that's why they invented the many ways to make things transactional, so you wouldn't have to worry about such things.
Raga Baskaran

Joined: Sep 16, 2010
Posts: 3
Thanks for responding.
Yes i can track the entries and exits of the processes using AOP/advise which calls my method. But my idea here to write a JSP to find out the open connections at any given instant ordered by connection creation time in descending order.
This page will display the caller class & method and the connection creation time, and this can help me in determining the leaks.

Since datasource is a container managed object, Does the container provide a way to view such info or a similar tool in admin console to see ?

Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46397
Welcome to the Ranch , Raga Baskaran
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

Decorator pattern. Extend the datasource type, override the getConnection and close methods to add logging then call the superclass. Note you may need to package your class somewhere the server can see it or you'll have visibility issues. That is if you declare the datasource in your server then the code will need to be in with the server common code.
I agree. Here's the link:
subject: DB Connection object tracking
It's not a secret anymore!