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

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

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

    codefans

    導航

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

    統計

    常用鏈接

    留言簿(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 春雷的博客 閱讀(196) 評論(0)  編輯  收藏


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


    網站導航:
     
    主站蜘蛛池模板: a级毛片高清免费视频| 日本免费无遮挡吸乳视频电影| 77777亚洲午夜久久多喷| 亚洲午夜久久久影院伊人| 成人毛片18女人毛片免费96| 久久成人18免费网站| 亚洲成AV人片在WWW| 99久久婷婷国产综合亚洲| 亚洲资源在线观看| 亚洲A∨无码一区二区三区| 亚洲AV无码不卡在线观看下载| 午夜福利不卡片在线播放免费| 免费很黄无遮挡的视频毛片| 亚洲精品伊人久久久久| 亚洲日韩在线视频| 亚洲一区中文字幕久久| 亚洲精品无码AV人在线播放| 午夜亚洲av永久无码精品| 2021国产精品成人免费视频| 99在线免费视频| 中文字幕无线码免费人妻| 杨幂最新免费特级毛片| 亚洲色丰满少妇高潮18p| xxx毛茸茸的亚洲| 亚洲欧美熟妇综合久久久久| 亚洲精品影院久久久久久| 亚洲嫩模在线观看| 亚洲成色在线影院| 亚洲91精品麻豆国产系列在线| 91亚洲一区二区在线观看不卡| 亚洲国产精品国自产拍AV| 亚洲熟妇无码八AV在线播放| 免费人成无码大片在线观看| 日韩中文字幕在线免费观看| 精品久久久久成人码免费动漫| 99爱在线观看免费完整版| 久久精品国产大片免费观看| 无码国产精品一区二区免费vr | 99精品在线免费观看| 国产精品永久免费10000| 无人在线直播免费观看|