/**
* Statement的自封装,屏蔽了getResultSet,executeQuery,getGeneratedKeys方法 返回自己的接管类
* 目的适记录SQl的动作和设置相应连接最后活动时间。
* @author Liudong
*/
package com.drsl.db;
import Java.io.*;
import java.sql.*;
import java.util.*;
import java.util.Date;
import java.lang.reflect.*;
import sun.jdbc.odbc.*;
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){
this.stm=stm;
}
public Statement getStatement(){
if(stm_proxy==null){
ClassLoader classloader=stm.getClass().getClassLoader();
Class[] interfaces = stm.getClass().getInterfaces();
if(interfaces==nullinterfaces.length==0){
interfaces = new Class[1];
interfaces[0] = Statement.class;
}
try{
stm_proxy= (Statement)Proxy.newProxyInstance(classloader,interfaces,this);
}catch(NullPointerException e){
log(e,"StatementObject 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("StatementObject--invoke:Method: \""+m.getName()+"\"");
//判定是否调用了getResultSet or executeQuery or getGeneratedKeys
//是就截获
if(GETRESULTSET_METHOD_NAME.equals(m.getName())
EXECUTEQUERY_METHOD_NAME.equals(m.getName())
GETGENERATEDKEYS_METHOD_NAME.equals(m.getName())){
ResultSet rs=(ResultSet)m.invoke(stm, args);
if(rs!=null && rso==null){
rso=new ResultSetObject(rs);
obj=rso.getResultSet();
}else if(rso!=null)
obj=rso.getResultSet();
else
log("StatementObject--invoke:Method: \""+m.getName()+"\"--失败");
}else{
obj = m.invoke(stm, args);
}
//设置最后一次访问时间,以便及时清除超时的连接
setLastAccessTime( new Date().getTime());
return obj;
}
/**
* 将文本信息写入日志文件
*/
private void log(String msg) {
ConnectionManager.log(msg);
}
/**
* 将文本信息与异常写入日志文件
*/
private void log(Throwable e, String msg) {
ConnectionManager.log(e,msg);
}
//设置最后一次访问时间
private void setLastAccessTime(long ltime){
ConnectionObject.setLastAccessTime(ltime);
}
}
/////////////////////////////////////////////////////////////////////////////////
/**
* PreparedStatement 的自封装,屏蔽了executeQuery方法 返回自己的接管类
* 目的适记录SQl的动作和设置相应连接最后活动时间。
* @author Liudong
*/
package com.drsl.db;
import java.io.*;
import java.sql.*;
import java.util.*;
import java.util.Date;
import java.lang.reflect.*;
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){
this.ps=ps;
}
public PreparedStatement getPreparedStatement(){
if(ps_proxy==null){
ClassLoader classloader=ps.getClass().getClassLoader();
Class[] interfaces = ps.getClass().getInterfaces();
if(interfaces==nullinterfaces.length==0){
interfaces = new Class[1];
interfaces[0] = PreparedStatement.class;
}
try{
ps_proxy= (PreparedStatement) Proxy.newProxyInstance(classloader,interfaces,this);
}catch(NullPointerException e){
log(e,"PreparedStatementObject 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("PreparedStatementObject--invoke:Method: \""+m.getName()+"\"");
//是否调用了executeQuery 假如是 则接管
if(EXECUTEQUERY_METHOD_NAME.equals(m.getName())){
ResultSet rs=(ResultSet)m.invoke(ps, args);
if(rs!=null && rso==null){
rso=new ResultSetObject(rs);
obj=rso.getResultSet();
}else if(rso!=null)
obj=rso.getResultSet();
else
log("PreparedStatementObject--invoke:Method: \""+m.getName()+"\"--失败");
}else{
obj = m.invoke(ps, args);
}
obj = m.invoke(ps, args);
//设置最后一次访问时间,以便及时清除超时的连接
setLastAccessTime( new Date().getTime());
return obj;
}
/**
* 将文本信息写入日志文件
*/
private void log(String msg) {
ConnectionManager.log(msg);
}
/**
* 将文本信息与异常写入日志文件
*/
private void log(Throwable e, String msg) {
ConnectionManager.log(e,msg);
}
//设置最后一次访问时间
private void setLastAccessTime(long ltime){
ConnectionObject.setLastAccessTime(ltime);
}
}
////////////////////////////////////////////////////////////////////////
/**
* Statement的自封装,屏蔽了getStatement方法 返回自己的接管类
* 目的适记录SQl的动作和设置相应连接最后活动时间。
* @author Liudong
*/
package com.drsl.db;
import java.io.*;
import java.sql.*;
import java.util.*;
import java.util.Date;
import java.lang.reflect.*;
public class ResultSetObject implements InvocationHandler{
private ResultSet rs=null;
privateResultSet rs_proxy =null