為什么要使用連接池?
          連接池一般比直接連接更有優(yōu)越性因為它提高了性能的同時還保存了寶貴的資源。打開數(shù)據(jù)庫連接時CPU和網(wǎng)絡(luò)的重要任務(wù)因此,在整個應(yīng)用程序的使用過程當中重復的打開直接連接將導致性能的下降。而池連接只在服務(wù)器啟動時打開一次,從而消除了這種性能問題。另外,因為連接只用于很短的時間,所以,連接可以被有效共享,而且有關(guān)連接參數(shù)的特有信息,只對池驅(qū)動程序有效,如數(shù)據(jù)庫用戶名稱和密碼,從而增強了系統(tǒng)的安全性和可管理性。
          打個比方:
          一輛汽車想從河的此岸到彼岸,這之間沒有橋,怎么辦呢?架橋吧。
          橋架好了,車過去了。
          然后呢?這個橋是拆掉呢?還是接著供以后使用呢?
          如果是拆掉,那么就相當于數(shù)據(jù)庫的直連,數(shù)據(jù)庫的每一個連接,都需要架一所橋,數(shù)據(jù)操作之后也就是說“車”通過之后,拆掉此橋。
          如果是保留,那么就相當于使用了“連接池”技術(shù),不過也并非“一座橋”這么簡單。

          主要思想:
            是基于JDBC而來的
            JDBC方法:開啟一個Connection,使用一個連接,關(guān)閉一個連接。
                        再次操作重復上面的。
                        而開啟一個連接對象浪費很多服務(wù)器資源。
            數(shù)據(jù)庫連接池:通過參數(shù)配置(參數(shù)看你需要綜合考慮)開啟N個連接 放在容器中(Tomcat,weblogic等)
                        使用連接,釋放連接。注意不是關(guān)閉,從新放回容器中,等待下次使用,無需再次開啟連接。


          實現(xiàn)其基本功能的核心代碼:

           1 package com.linying;
           2 
           3 import java.sql.Connection;
           4 import java.sql.DriverManager;
           5 import java.sql.SQLException;
           6 import java.util.LinkedList;
           7 
           8 /**
           9  * 加工數(shù)據(jù)源,實現(xiàn)連接池
          10  * @author Ying-er
          11  * @time 2010-2-1下午06:47:03
          12  * @version 1.0
          13  */
          14 public class MyDataSource {
          15     /**
          16      * 指定數(shù)據(jù)庫的URL
          17      */
          18     private String url = "jdbc:mysql://127.0.0.1/test";
          19 
          20     /**
          21      * 指定登陸用戶名
          22      */
          23     private String user = "root";
          24 
          25     /**
          26      * 指定登陸用戶的密碼
          27      */
          28     private String password = "1234";
          29 
          30     /**
          31      * 初始連接數(shù)(一次性創(chuàng)建50個連接)
          32      */
          33     private int initCount = 50;
          34 
          35     /**
          36      * 記錄當前連接數(shù)
          37      */
          38     int currentCount = 0;
          39 
          40     /**
          41      * 使用LinkedList的connectionPool存儲MyConnection對象
          42      */
          43     LinkedList<Connection> connectionPool = new LinkedList<Connection>();
          44 
          45     /**
          46      * 構(gòu)造函數(shù) 創(chuàng)建MyDataSource時創(chuàng)建initCount個連接
          47      * 
          48      */
          49     public MyDataSource() {
          50         try {
          51             for (int i = 0; i < initCount; i++) {
          52                 this.connectionPool.addLast(this.createConnection());
          53                 this.currentCount++;
          54             }
          55         } catch (SQLException e) {
          56             throw new ExceptionInInitializerError(e);
          57         }
          58     }
          59 
          60     /**
          61      * 創(chuàng)建連接
          62      * 
          63      * @return
          64      * @throws SQLException
          65      */
          66     private Connection createConnection() throws SQLException {
          67         Connection realConnection = DriverManager.getConnection(url, user,
          68                 password);
          69         MyConnection myConnection = new MyConnection(realConnection, this);
          70         return myConnection;
          71     }
          72 
          73     /**
          74      * 從連接池里獲得連接
          75      * 
          76      * @return
          77      * @throws SQLException
          78      */
          79     public Connection getConnection() throws SQLException {
          80         synchronized (connectionPool) {
          81             if (this.connectionPool.size() > 0) {
          82                 return this.connectionPool.removeFirst();
          83             } else {
          84                 this.currentCount++;
          85                 return this.createConnection();
          86             }
          87 
          88         }
          89     }
          90 
          91     /**
          92      * 釋放連接
          93      * 
          94      * @param conn
          95      */
          96     public void free(Connection conn) {
          97         this.connectionPool.addLast(conn);
          98     }
          99 }

           1 package com.linying;
           2 
           3 import java.sql.Array;
           4 import java.sql.Blob;
           5 import java.sql.CallableStatement;
           6 import java.sql.Clob;
           7 import java.sql.Connection;
           8 import java.sql.DatabaseMetaData;
           9 import java.sql.NClob;
          10 import java.sql.PreparedStatement;
          11 import java.sql.SQLClientInfoException;
          12 import java.sql.SQLException;
          13 import java.sql.SQLWarning;
          14 import java.sql.SQLXML;
          15 import java.sql.Savepoint;
          16 import java.sql.Statement;
          17 import java.sql.Struct;
          18 import java.util.Map;
          19 import java.util.Properties;
          20 /**
          21  * 自定義Connection
          22  * @author Ying-er
          23  * @time 2010-2-1下午06:36:24
          24  * @version 1.0
          25  */
          26 public class MyConnection implements Connection{
          27     
          28     private Connection realConnection;
          29     private MyDataSource dataSource;
          30     private int maxUserCount=200;
          31     private int currentUserCount=0;
          32     
          33     MyConnection(Connection connection,MyDataSource dataSource){
          34         this.realConnection=connection;
          35         this.dataSource=dataSource;
          36     }
          37 
          38     public void clearWarnings() throws SQLException {
          39         // TODO Auto-generated method stub
          40         this.realConnection.clearWarnings();
          41         
          42     }
          43 
          44     /**
          45      * 重寫close方法,
          46      * close的時候
          47      * 將此已用過的數(shù)據(jù)源添加到LinkedList尾部
          48      * 此LinkedList在MyDataSource文件中定義
          49      * 注意:目的是釋放而不是不是關(guān)閉數(shù)據(jù)源
          50      */
          51     public void close() throws SQLException {
          52         // TODO Auto-generated method stub
          53         this.currentUserCount++;
          54         if(this.currentUserCount<this.maxUserCount){
          55             this.dataSource.connectionPool.addLast(this);
          56         }
          57         else{
          58             this.realConnection.close();
          59             this.dataSource.currentCount--;
          60         }
          61         
          62     }
          63     
          64     /**
          65      * 實現(xiàn)Connection中的所有方法
          66      * ……
          67      */
          68 
          69     public void commit() throws SQLException {
          70         // TODO Auto-generated method stub
          71         this.realConnection.commit();
          72         
          73     }
          74     /**
          75      * 以下代碼略掉
          76      */

          此實例工程下載地址:
          http://www.aygfsteel.com/Files/crazycoding/DBConnectionPool.rar

          注:只是個人研究成果,有錯誤和不準確之處請諒解。

          posted on 2010-02-01 19:06 Ying-er 閱讀(373) 評論(0)  編輯  收藏

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


          網(wǎng)站導航:
           
          主站蜘蛛池模板: 海阳市| 松溪县| 英德市| 六盘水市| 沙雅县| 吴忠市| 延长县| 公安县| 鄂温| 邛崃市| 开封市| 海晏县| 祁东县| 乐安县| 开远市| 柳河县| 定结县| 民和| 瑞昌市| 牟定县| 青海省| 如东县| 开封市| 焦作市| 汝阳县| 莱阳市| 保靖县| 耒阳市| 大渡口区| 广宗县| 云林县| 竹北市| 八宿县| 成武县| 铜陵市| 五台县| 大方县| 海宁市| 静海县| 咸丰县| 新巴尔虎右旗|