下午没事做,第一次尝试着写数据连接池。
想到了大概几点:1.使用单例模式;2.在构造方法中将数据源初始化;3.大概包括几个方法:init()、destroy()、getConnect()、release()
代码如下:
1.连接池类:
1public class DBPool {
2
3 /** *//**
4 * 用个集合来装连接
5 */
6 private Vector<Connection> pool;
7 /** *//**
8 * 驱动名字
9 */
10 private String driverClassName;
11 /** *//**
12 * url
13 */
14 private String url;
15 /** *//**
16 * 用户名
17 */
18 private String userName;
19 /** *//**
20 * 密码
21 */
22 private String passWord;
23 /** *//**
24 * 连接池大小
25 */
26 private int poolSize;
27
28 /** *//**
29 * 单例模式
30 */
31 private static DBPool instance = null;
32
33 /** *//**
34 * 获取对象
35 *
36 * @return
37 */
38 public static synchronized DBPool getInstance() {
39 if (instance == null) {
40 instance = new DBPool();
41 }
42 return instance;
43 }
44
45 /** *//**
46 * 构造方法+初始化
47 */
48 private DBPool() {
49 // DBSourceBean,装driver,url,username,password和一些初始化方法
50 DBSourceBean dbSource = new DBSourceBean();
51
52 // 将从DBSourceBean获取的数据源对象的值赋值
53 this.driverClassName = dbSource.getDriverClassName();
54 this.url = dbSource.getUrl();
55 this.userName = dbSource.getUserName();
56 this.passWord = dbSource.getPassWord();
57 this.poolSize = dbSource.getPoolSize();
58
59 // 初始化
60 init();
61 }
62
63 /** *//**
64 * 初始化
65 */
66 private void init() {
67 // 创建大小为连接池大小的Vector
68 pool = new Vector<Connection>(poolSize);
69
70 // 将连接装到Vector中去
71 addConnection();
72 }
73
74 /** *//**
75 * 制造连接池
76 */
77 private void addConnection() {
78 Connection conn = null;
79 try {
80 for (int i = 0; i < poolSize; i++) {
81
82 // 用jakarta commons的db的组件简化
83 DbUtils.loadDriver(driverClassName);
84 // 获取连接
85 conn = DriverManager.getConnection(url, userName, passWord);
86 // 将连接加入到Vector中去
87 pool.add(conn);
88
89 }
90 } catch (Exception e) {
91 e.printStackTrace();
92 }
93 }
94
95 /** *//**
96 * 外部获取连接使用的方法
97 *
98 * @return
99 */
100 public synchronized Connection getConnection() {
101 if (pool.size() > 0) {
102 Connection conn = pool.get(0);
103 pool.remove(conn);
104 return conn;
105 } else {
106 return null;
107 }
108 }
109
110 /** *//**
111 * 销毁连接池
112 */
113 public synchronized void destroy() {
114 try {
115 if (pool != null) {
116 for (int i = 0; i < pool.size(); i++) {
117 ((Connection) pool.get(i)).close();
118 pool.remove(i);
119 }
120 }
121 } catch (Exception e) {
122 e.printStackTrace();
123 }
124
125 }
126
127 /** *//**
128 * 释放一个连接
129 *
130 * @param conn
131 */
132 public synchronized void release(Connection conn) {
133 pool.add(conn);
134 }
135
136}
137//set,get方法省略
2.数据源的bean类
1public class DBSourceBean {
2 /** *//**
3 * 驱动类名
4 */
5 private String driverClassName;
6 /** *//**
7 * url
8 */
9 private String url;
10 /** *//**
11 * 用户名
12 */
13 private String userName;
14 /** *//**
15 * 密码
16 */
17 private String passWord;
18 /** *//**
19 * 连接池大小
20 */
21 private int poolSize;
22
23 /** *//**
24 * 读取配置文件
25 */
26 private void readConfig() {
27 FileInputStream fis = null;
28 Properties props = null;
29 String path = "";
30 try {
31 path = Thread.currentThread().getContextClassLoader().getResource("").toString();
32 path += "\\dataSource.properties";
33 path = path.substring("file:\\".length());
34 fis = new FileInputStream(path);
35 props = new Properties();
36 props.load(fis);
37 this.driverClassName = props.getProperty("driverClassName");
38 this.url = props.getProperty("url");
39 this.userName = props.getProperty("userName");
40 this.passWord = props.getProperty("passWord");
41 this.poolSize = Integer.parseInt(StringUtil.nvm(props.getProperty("poolSize"), "1"));
42 } catch (FileNotFoundException e) {
43 e.printStackTrace();
44 } catch (IOException e) {
45 e.printStackTrace();
46 }
47 }
48}
49//set,get方法省略
3.属性文件dataSource.properties
driverClassName=oracle.jdbc.driver.OracleDriver
userName=scott
passWord=tiger
url=jdbc:oracle:thin:@localhost:1521:sure
poolSize=10
4.测试类
1public class TestDB {
2
3 public static void main(String[] args) {
4
5 DBPool db = null;
6 Connection conn = null;
7 String sql = "select empno,ename from emp";
8
9 try {
10 long first = System.currentTimeMillis();
11 System.out.println("------111111开始计时-------");
12 // for (int j = 0; j < 10; j++) {
13 // QueryRunner qr = new QueryRunner();
14 // String sql = "select empno,ename from emp";
15 // List results = (List) qr.query(conn, sql, new MapListHandler());
16 //
17 //
18 // for (int i = 0; i < results.size(); i++) {
19 // Map map = (Map) results.get(i);
20 // }
21 // }
22 for (int i = 0; i < 100; i++) {
23 db = DBPool.getInstance();
24 conn = db.getConnection();
25 Statement stmt = conn.createStatement();
26 ResultSet rs = stmt.executeQuery(sql);
27 while (rs.next()) {
28 }
29 rs.close();
30 stmt.close();
31 db.release(conn);
32 }
33 long last = System.currentTimeMillis();
34 System.out.println("------111111共耗时" + (last - first) + "-------");
35
36
37 /** *//**
38 * 测试非连接池的
39 */
40 String driverName = "oracle.jdbc.driver.OracleDriver";
41 String url = "jdbc:oracle:thin:@localhost:sure";
42 String userName = "scott";
43 String passWord = "tiger";
44 first = System.currentTimeMillis();
45 System.out.println("------222222开始计时-------");
46 for (int i = 0; i < 100; i++) {
47 Class.forName(driverName);
48 Connection conn1 = DriverManager.getConnection(url, userName,
49 passWord);
50 Statement stmt1 = conn1.createStatement();
51 ResultSet rs1 = stmt1.executeQuery(sql);
52 while (rs1.next()) {
53 }
54 rs1.close();
55 conn1.close();
56 stmt1.close();
57 }
58
59 last = System.currentTimeMillis();
60 System.out.println("------2222222共耗时" + (last - first) + "-------");
61
62 } catch (ClassNotFoundException e) {
63 e.printStackTrace();
64 } catch (SQLException e) {
65 e.printStackTrace();
66 }
67 }
68}
69
5.总结:功能还很不完善,连接池和管理连接池可以分开写,还有一些其他的欠考虑,数据源可以用xml来存放,等等。。想好了再改
posted on 2008-07-31 22:16
xrzp 阅读(230)
评论(0) 编辑 收藏 所属分类:
JAVA