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

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

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

    codefans

    導航

    <2025年5月>
    27282930123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567

    統計

    常用鏈接

    留言簿(2)

    隨筆分類

    隨筆檔案

    文章分類

    文章檔案

    程序設計鏈接

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    定義自己的Connection Pool

     選擇自 small_ding 的 Blog

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

    1.存儲基本的數據庫連接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("沒有提供驅動程序名稱!");

        if(url==null)

          throw new Exception("沒有提供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+"個連接");

           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("關閉第"+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 春雷的博客 閱讀(195) 評論(0)  編輯  收藏


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 久久精品国产免费一区| 国产亚洲精彩视频| 日本免费一区二区三区| 国产aⅴ无码专区亚洲av麻豆| 337P日本欧洲亚洲大胆精品| 大学生一级特黄的免费大片视频 | 亚洲看片无码在线视频| 4虎永免费最新永久免费地址| 亚洲一区二区三区亚瑟| 日本三级2019在线观看免费| 最新亚洲精品国偷自产在线| 在线免费观看视频你懂的| 特级一级毛片免费看| 亚洲精品亚洲人成在线观看| 久久久久久久岛国免费播放| 亚洲成人网在线观看| 在线观看成人免费视频| 羞羞的视频在线免费观看| 超清首页国产亚洲丝袜| 久久久免费精品re6| 国产成人精品日本亚洲专区6| 国产精品无码素人福利免费| 一级看片免费视频| 中文字幕亚洲精品资源网| 扒开双腿猛进入爽爽免费视频 | 中文字幕在线日亚洲9| 国产在线a不卡免费视频| 拍拍拍无挡免费视频网站| 亚洲欧洲av综合色无码| 国产一精品一aⅴ一免费| 97在线免费视频| 亚洲黄色片在线观看| 日韩高清在线高清免费| 国产免费久久久久久无码| 亚洲综合视频在线观看| jjzz亚洲亚洲女人| 18禁男女爽爽爽午夜网站免费| 亚洲AV无码AV日韩AV网站| 久久亚洲国产成人亚| 日本人护士免费xxxx视频| 四虎影视成人永久免费观看视频|