<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    codefans

    導(dǎo)航

    <2025年7月>
    293012345
    6789101112
    13141516171819
    20212223242526
    272829303112
    3456789

    統(tǒng)計(jì)

    常用鏈接

    留言簿(2)

    隨筆分類(lèi)

    隨筆檔案

    文章分類(lèi)

    文章檔案

    程序設(shè)計(jì)鏈接

    搜索

    最新評(píng)論

    閱讀排行榜

    評(píng)論排行榜

    定義自己的Connection Pool

     選擇自 small_ding 的 Blog

    自定義Connection Pool的源代碼及用法

    1.存儲(chǔ)基本的數(shù)據(jù)庫(kù)連接Bean

    package BSC.pool;

    import java.sql. *;

    import java.io.Serializable;

     

    /**

     * <p>Title: </p>

     * <p>Description: </p>

     * <p>Copyright: Copyright (c) 2003</p>

     * <p>Company: </p>

     * @author unascribed

     * @version 1.0

     */

     

    public class ConnBean implements java.io.Serializable {

     

      private Connection conn=null;

      private boolean inuse=false;

      public ConnBean() {

      }

      public ConnBean(Connection con)

      {

        if(con!=null)conn=con;

      }

      public void setConnection(Connection con)

      {

        conn=con;

      }

      public Connection getConnection()

      {

        return conn;

      }

      public void setInuse(boolean inuse)

      {

        this.inuse =inuse;

      }

      public boolean getInuse()

      {

        return inuse;

      }

      public void close()

      {

        try

        {

          conn.close();

        }

        catch(SQLException sqle)

        {

          System.err.println(sqle.getMessage());

        }

      }

    }

    2. 連接池管理Bean

     

    package BSC.pool;

    import java.sql. *;

    import java.util.*;

    import java.lang.InterruptedException;

    import java.io.Serializable;

     

    import BSC.pool.ConnBean;

    /**

     * <p>Title: </p>

     * <p>Description: </p>

     * <p>Copyright: Copyright (c) 2003</p>

     * <p>Company: </p>

     * @author unascribed

     * @version 1.0

     */

     

    public class PoolBean  implements java.io.Serializable {

     

      private String driver=null;

      private String url=null;

      private int size=0;

      private String username="";

      private String password="";

      private ConnBean connBean=null;

      private Vector pool=null;

      private String dbType="1";

      public PoolBean() {

      }

      public void setDriver(String d)

      {

        if(d!=null)driver=d;

      }

      public String getDriver()

      {

        return driver;

      }

      public void setURL(String u)

      {

        if(u!=null)url=u;

      }

      public String getURL()

      {

        return url;

      }

      public void setSize(int s)

      {

        if(s>1)size=s;

      }

      public int getSize()

      {

        return size;

      }

      public void setUsername(String un)

      {

        if(un!=null)username=un;

      }

      public String getUsername()

      {

        return username;

      }

      public void setPassword(String pwd)

      {

        if(pwd!=null)password=pwd;

      }

      public String getPassword()

      {

        return password;

      }

      public void setConnBean(ConnBean cb)

      {

        if(cb!=null)connBean=cb;

      }

      public ConnBean getConnBean() throws Exception

      {

        Connection con=getConnection();

        ConnBean cb=new ConnBean(con);

        cb.setInuse(true) ;

        return cb;

     

      }

      private Connection createConnection() throws Exception

      {

        Connection con=null;

        con=DriverManager.getConnection(url,username,password) ;

        return con;

      }

      public synchronized void initializePool() throws Exception

      {

        if(driver==null)

          throw new Exception("沒(méi)有提供驅(qū)動(dòng)程序名稱!");

        if(url==null)

          throw new Exception("沒(méi)有提供URL");

        if(size<1)

          throw new Exception("連接池大小不能小于一!");

        try

        {

            // DriverManager.registerDriver(new COM.ibm.db2.jdbc.app.DB2Driver());

             Class.forName(driver).newInstance();

     

          for(int i=0;i<size;i++)

          {

            Connection con=createConnection();

            if(con!=null)

            {

              ConnBean cb=new ConnBean(con);

              addConnection(cb);

     

            }

          }

        }

        catch(Exception e)

        {

         System.err.println(e.getMessage());

         throw new Exception(e.getMessage() );

        }

     

      }

      private void addConnection(ConnBean cb)

     {

       if(pool==null)pool=new Vector(size);

        pool.addElement(cb);

     }

     

     public synchronized void releaseConnection(Connection con)

     {

       for(int i=0;i<pool.size() ;i++)

       {

         ConnBean cb=(ConnBean)pool.elementAt(i);

         if(cb.getConnection() ==con)

         {

           System.err.println("釋放第"+i+"個(gè)連接");

           cb.setInuse(false);

           break;

         }

       }

     }

     public synchronized Connection getConnection()

     throws Exception

     {

       ConnBean  cb=null;

       for(int i=0;i<pool.size() ;i++)

       {

         cb=(ConnBean)pool.elementAt(i);

         if(cb.getInuse()==false)

         {

           cb.setInuse(true) ;

           Connection con=cb.getConnection();

           if(!con.isClosed()){

             return con;

           }else{

             pool.removeElement(cb);

           }

         }

       }

       try

       {

         Connection con=createConnection();

         cb=new ConnBean(con);

         cb.setInuse(true);

         pool.addElement(cb);

     

       }

       catch(Exception e)

       {

         System.err.println(e.getMessage() );

         throw new Exception(e.getMessage() );

       }

       return cb.getConnection() ;

     }

     

     public synchronized void emptyPool()

     {

      for(int i=0;i<pool.size() ;i++)

      {

        System.err.println("關(guān)閉第"+i+"JDBC連接");

        ConnBean cb=(ConnBean)pool.elementAt(i);

        if(cb.getInuse()==false)

              cb.close();

        else

        {

          try

          {

             java.lang.Thread.sleep(20000);

             cb.close();

          }

          catch(InterruptedException ie)

          {

            System.err.println(ie.getMessage());

          }

        }

      }

     }

      public String getDbType() {

        return dbType;

      }

      public void setDbType(String dbType) {

        this.dbType = dbType;

      }

    }

    3.用法

    import java.sql.*;

    import BSC.pool.*;

    其他代碼

    Connection conn=null;

    try{

            poolbean = new PoolBean();

            poolbean.setDriver(this.getDriver());

            poolbean.setURL(this.getUrl());

            poolbean.setSize((new Integer(this.getSize())).intValue());

            poolbean.setUsername(this.getUsername());

            poolbean.setPassword(this.getPassword());

            poolbean.initializePool();

            conn=pool.getConnection();

            其他代碼

    }catch(Exception e){

            e.printStackTrace();

    }finally{

            try{

           if(conn!=null)

               conn.close();

    }catch(SQLException sqle){

          sqle.printStackTrace();

     

    }

    }

    posted on 2005-11-22 11:24 春雷的博客 閱讀(196) 評(píng)論(0)  編輯  收藏


    只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 国产无人区码卡二卡三卡免费| 国产大片免费天天看| 性xxxx视频免费播放直播| 四虎成人精品在永久免费| 亚洲国产欧美一区二区三区| 91免费国产在线观看| 亚洲欧洲国产综合| 国内精自视频品线六区免费| 亚洲国产成人精品电影| 波多野结衣在线免费观看| 亚洲丰满熟女一区二区v| 毛色毛片免费观看| 亚洲精品色在线网站| 免费人妻av无码专区| 东北美女野外bbwbbw免费| 亚洲不卡av不卡一区二区| 国内精品免费视频精选在线观看| 亚洲AV综合色区无码一区 | 国产亚洲精久久久久久无码| 免费91麻豆精品国产自产在线观看 | 怡红院免费的全部视频| 久久精品国产亚洲香蕉| 亚洲免费闲人蜜桃| 亚洲大尺度无码无码专线一区| 亚洲 自拍 另类小说综合图区 | 人人鲁免费播放视频人人香蕉| 亚洲色婷婷六月亚洲婷婷6月| 国产在线一区二区综合免费视频| 亚洲成人一级电影| 国产午夜影视大全免费观看| 久久精品成人免费观看97| 亚洲黄色免费电影| 国产精品免费视频网站| 青青操免费在线视频| 久久精品国产99国产精品亚洲| 国产成人精品高清免费| 国产日韩一区二区三免费高清| 亚洲jjzzjjzz在线播放| 亚洲精品无码久久久| 亚洲一级毛片免费看| 九九全国免费视频|