September 22, 2006

ResultSet Proxy

/**

This example on how you can use the Java Proxy class to intercept the close()
method on a ResultSet and then call close() the statement and the connection. This is usefull if you want to have a util function return a ResultSet but not have to worry about leaking the connection
*/

import java.lang.reflect.*;
import java.sql.*;


public class ResultSetProxy implements InvocationHandler {
private Connection c;
private Statement s;
private ResultSet r;

public ResultSetProxy(Connection c,Statement s,ResultSet r) {
this.c = c;
this.s = s;
this.r = r;
}

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Object res = method.invoke( r , args );
if ( method.getName().equals("close") ) {
close(c,s);
}
return res;
}
}

No comments: