This document describes the JDBC Resource Monitor Toolkit, what it does and how to use it.
Typically a situation would exist where a connection is made to a database, a statement is created and a query is executed resulting in the creation of a result set object. Though it is the developers responsiblity to close these resources once they have been finished with, it is not always the case and our database/database access may begin to perform badly and in some situations fail.
I created this project because that situation often manifests itself in the projects I am used to working on. The development team is usually fairly large and under tight time-scales and quality of code is usually the first to suffer. In addition the database access is often being done in a heavily used multi-threaded environment, that is to say a J2EE environment made of servlets, JSPs, etc.
In order to combat this I have wanted to write a utility library to help monitor these resources and this project is the result.
When a new Statement is created or query executed the status of the current JDBC environment is printed to a specified output stream (this defaults to standard output).
A method can be called on the main monitor singleton (jdbcmon.JDBCMonitor) to dump some extra information about the objects that are currently open. At the moment that information amounts to the Object itself, the time it was created and, when required (by the use of a properties file), the line of code that the resource was opened on.
Finding the line of code the resource was opened on is achieved by taking a dump of the stack at the moment the object was created and then parsing the stack for the last occurence of a jdbcmon package identifier. The line that follows it is considered to be the line that created the resource. In the event this cannot be achieved an error message is displayed in it's place.
The follow example shows simple usage:
private static Connection establishConnection(String driver, String url, String user, String password) { try { Class.forName(driver); /* Construct a ConnectionMontior. */ Connection conn = DriverManager.getConnection(url, user, password); conn = new ConnectionMonitor(conn); return conn; } catch(ClassNotFoundException ex) { ex.printStackTrace(); return null; } catch(SQLException ex) { ex.printStackTrace(); return null; } }From that point on Statement objects, ResultSet objects, etc will be created with a jdbcmon Object of the appropriate type. Each of these objects signals it's status to the JDBCMonitor singleton, which in turn dumps the info to the specified output stream.
This information can be dumped at any time using the following code:
JDBCMonitor.getInstance().status();The extra information can be dumped by using the following code:
JDBCMonitor.getInstance().extraInfo();Which will show a list of currently open resources.
Though this project is covered by the Lesser GNU Public License (see
license.txt) I would ask that if you decided to use this library in a project
that you drop me a line either by email or (preferably on a post card) by post.