Question

I want to create a subclass that extends the CallableStatement object. I want to do this so I can override the execute and executeQuery methods to track some metrics on each SP call.

Currently I have code that looks like this:

Connection db = poolingDataSource.getConnection();
CallableStatement cstmt = db.prepareCall("{call pSampleStoredProc()}");
ResultSet rs = cstmt.executeQuery();

where poolingDataSource is from the apache commons dbcp package. My implementation is hooked into a MySQL database using JDBC.

Currently, the prepareCall method returns a com.mysql.jdbc.JDBC4CallableStatement. I'd like to be able to change this so that it returns a class of my own that extends JDBC4CallableStatement but overrides the execute() and executeQuery() methods.

Any ideas about the best way to do this?

Was it helpful?

Solution

If you have a manageable number of places where you need to do this, I suggest an easy way to do it (easier than trying to get that database driver to return your custom CallableStatement object) would be to make a wrapper or decorator to wrap over the CallableStatement object you get from db. Basically make a class that implements all the methods of CallableStatement, takes a CallableStatement object as a parameter to its constructor, and delegates all the method calls right through to the other object, with the exception that for execute() and executeQuery() you also call your logging methods before and after delegating to the other objects execute() method.

Certainly there are other ways to do this, this just struck me as easier.

Connection db = poolingDataSource.getConnection();
CallableStatement cstmt = db.prepareCall("{call pSampleStoredProc()}");
MyCallableStatement mystmt = new MyCallableStatement( cstmt );
ResultSet rs = mystmt.executeQuery();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top