为什么要使用连接池?
连接池一般比直接连接更有优越性因为它提高了性能的同时还保存了宝贵的资源。打开数据库连接时CPU和网络的重要任务因此,在整个应用程序的使用过程当中重复的打开直接连接将导致性能的下降。而池连接只在服务器启动时打开一次,从而消除了这种性能问题。另外,因为连接只用于很短的时间,所以,连接可以被有效共享,而且有关连接参数的特有信息,只对池驱动程序有效,如数据库用户名称和密码,从而增强了系统的安全性和可管理性。
打个比方:
一辆汽车想从河的此岸到彼岸,这之间没有桥,怎么办呢?架桥吧。
桥架好了,车过去了。
然后呢?这个桥是拆掉呢?还是接着供以后使用呢?
如果是拆掉,那么就相当于数据库的直连,数据库的每一个连接,都需要架一所桥,数据操作之后也就是说“车”通过之后,拆掉此桥。
如果是保留,那么就相当于使用了“连接池”技术,不过也并非“一座桥”这么简单。

主要思想:
  是基于JDBC而来的
  JDBC方法:开启一个Connection,使用一个连接,关闭一个连接。
              再次操作重复上面的。
              而开启一个连接对象浪费很多服务器资源。
  数据库连接池:通过参数配置(参数看你需要综合考虑)开启N个连接 放在容器中(Tomcat,weblogic等)
              使用连接,释放连接。注意不是关闭,从新放回容器中,等待下次使用,无需再次开启连接。


实现其基本功能的核心代码:

 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  * 加工数据源,实现连接池
10  * @author Ying-er
11  * @time 2010-2-1下午06:47:03
12  * @version 1.0
13  */
14 public class MyDataSource {
15     /**
16      * 指定数据库的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      * 初始连接数(一次性创建50个连接)
32      */
33     private int initCount = 50;
34 
35     /**
36      * 记录当前连接数
37      */
38     int currentCount = 0;
39 
40     /**
41      * 使用LinkedList的connectionPool存储MyConnection对象
42      */
43     LinkedList<Connection> connectionPool = new LinkedList<Connection>();
44 
45     /**
46      * 构造函数 创建MyDataSource时创建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      * 创建连接
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      * 将此已用过的数据源添加到LinkedList尾部
48      * 此LinkedList在MyDataSource文件中定义
49      * 注意:目的是释放而不是不是关闭数据源
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      * 实现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.blogjava.net/Files/crazycoding/DBConnectionPool.rar

注:只是个人研究成果,有错误和不准确之处请谅解。

posted on 2010-02-01 19:06 Ying-er 阅读(361) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: