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 ?
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.
Joined: Sep 16, 2010
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 ?
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.