aspose file tools*
The moose likes JDBC 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
Bookmark "DB Connection object tracking" Watch "DB Connection object tracking" New topic
Author

DB Connection object tracking

Raga Baskaran
Greenhorn

Joined: Sep 16, 2010
Posts: 3
Hi,

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 ?

Thanks.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19761
    
  20

Moving to JDBC.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
David Newton
Author
Rancher

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
Greenhorn

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 ?

Thanks.
Campbell Ritchie
Sheriff

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

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: http://aspose.com/file-tools
 
subject: DB Connection object tracking