/**
* Statement的自封装屏蔽了getResultSetexecuteQuerygetGeneratedKeys方法 返回自己的接管类
* 目的适记录SQl的动作和设置相应连接最后活动时间
* @author Liudong
*/
package comdrsldb;
import javaio*;
import javasql*;
import javautil*;
import javautilDate;
import javalangreflect*;
import sunjdbcodbc*;
public class StatementObject implements InvocationHandler{
private Statement stm=null;
privateStatement stm_proxy=null;
private final static String GETRESULTSET_METHOD_NAME = getResultSet;
private final static String EXECUTEQUERY_METHOD_NAME = executeQuery;
private final static String GETGENERATEDKEYS_METHOD_NAME = getGeneratedKeys;
private ResultSetObject rso=null;
public StatementObject(Statement stm){
thisstm=stm;
}
public Statement getStatement(){
if(stm_proxy==null){
ClassLoader classloader=stmgetClass()getClassLoader();
Class[] interfaces = stmgetClass()getInterfaces();
if(interfaces==null||interfaceslength==){
interfaces = new Class[];
interfaces[] = Statementclass;
}
try{
stm_proxy= (Statement)ProxynewProxyInstance(classloaderinterfacesthis);
}catch(NullPointerException e){
log(eStatementObject getStatement()error);
}
if(stm_proxy!=null)
log(StatementObject getStatement()success);
}
return stm_proxy;
}
public Object invoke(Object proxy Method m Object[] args)throws Throwable
{
Object obj = null;
log(StatementObjectinvoke:Method: \+mgetName()+\);
//判断是否调用了getResultSet or executeQuery or getGeneratedKeys
//是就截获
if(GETRESULTSET_METHOD_NAMEequals(mgetName())
|| EXECUTEQUERY_METHOD_NAMEequals(mgetName())
|| GETGENERATEDKEYS_METHOD_NAMEequals(mgetName())){
ResultSet rs=(ResultSet)minvoke(stm args);
if(rs!=null && rso==null){
rso=new ResultSetObject(rs);
obj=rsogetResultSet();
}else if(rso!=null)
obj=rsogetResultSet();
else
log(StatementObjectinvoke:Method: \+mgetName()+\失败);
}else{
obj = minvoke(stm args);
}
//设置最后一次访问时间以便及时清除超时的连接
setLastAccessTime( new Date()getTime());
return obj;
}
/**
* 将文本信息写入日志文件
*/
private void log(String msg) {
ConnectionManagerlog(msg);
}
/**
* 将文本信息与异常写入日志文件
*/
private void log(Throwable e String msg) {
ConnectionManagerlog(emsg);
}
//设置最后一次访问时间
private void setLastAccessTime(long ltime){
ConnectionObjectsetLastAccessTime(ltime);
}
}
/////////////////////////////////////////////////////////////////////////////////
/**
* PreparedStatement 的自封装屏蔽了executeQuery方法 返回自己的接管类
* 目的适记录SQl的动作和设置相应连接最后活动时间
* @author Liudong
*/
package comdrsldb;
import javaio*;
import javasql*;
import javautil*;
import javautilDate;
import javalangreflect*;
public class PreparedStatementObject implements InvocationHandler{
private PreparedStatement ps=null;
privatePreparedStatement ps_proxy =null;
private final static String EXECUTEQUERY_METHOD_NAME = executeQuery;
private ResultSetObject rso=null;
PreparedStatementObject(PreparedStatement ps){
thisps=ps;
}
public PreparedStatement getPreparedStatement(){
if(ps_proxy==null){
ClassLoader classloader=psgetClass()getClassLoader();
Class[] interfaces = psgetClass()getInterfaces();
if(interfaces==null||interfaceslength==){
interfaces = new Class[];
interfaces[] = PreparedStatementclass;
}
try{
ps_proxy= (PreparedStatement) ProxynewProxyInstance(classloaderinterfacesthis);
}catch(NullPointerException e){
log(ePreparedStatementObject getPreparedStatement()error);
}
if(ps_proxy!=null)
log(PreparedStatementObject getPreparedStatement()success);
}
return ps_proxy;
}
public Object invoke(Object proxy Method m Object[] args)throws Throwable
{
Object obj = null;
log(PreparedStatementObjectinvoke:Method: \+mgetName()+\);
//是否调用了executeQuery 如果是 则接管
if(EXECUTEQUERY_METHOD_NAMEequals(mgetName())){
ResultSet rs=(ResultSet)minvoke(ps args);
if(rs!=null && rso==null){
rso=new ResultSetObject(rs);
obj=rsogetResultSet();
}else if(rso!=null)
obj=rsogetResultSet();
else
log(PreparedStatementObjectinvoke:Method: \+mgetName()+\失败);
}else{
obj = minvoke(ps args);
}
obj = minvoke(ps args);
//设置最后一次访问时间以便及时清除超时的连接
setLastAccessTime( new Date()getTime());
return obj;
}
/**
* 将文本信息写入日志文件
*/
private void log(String msg) {
ConnectionManagerlog(msg);
}
/**
* 将文本信息与异常写入日志文件
*/
private void log(Throwable e String msg) {
ConnectionManagerlog(emsg);
}
//设置最后一次访问时间
private void setLastAccessTime(long ltime){
ConnectionObjectsetLastAccessTime(ltime);
}
}
////////////////////////////////////////////////////////////////////////
/**
* Statement的自封装屏蔽了getStatement方法 返回自己的接管类
* 目的适记录SQl的动作和设置相应连接最后活动时间
* @author Liudong
*/
package comdrsldb;
import javaio*;
import javasql*;
import javautil*;
import javautilDate;
import javalangreflect*;
public class ResultSetObject implements InvocationHandler{
private ResultSet rs=null;
privateResultSet rs_proxy =null