熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

自己寫的一個未成熟的數據庫連接池(二)

2022-06-13   來源: Java核心技術 

  /**
   * 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;
    private  Statement 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;
    private  PreparedStatement 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;
    private  ResultSet rs_proxy =null
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26864.html
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.