首页 > 其他 > 详细

编写一个基本的连接池来实现连接的复用&一些工程细节上的优化

时间:2014-09-04 00:10:17      阅读:354      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
 1 package it.cast.jdbc;
 2 
 3 import java.sql.Connection;
 4 import java.sql.DriverManager;
 5 import java.sql.SQLException;
 6 import java.util.LinkedList;
 7 
 8 public class MyDataSource {
 9 
10     private static String url = "jdbc:mysql://localhost:3306/jdbc";
11     private static String username = "root";
12     private static String password = "123";
13 
14     private static int initCount = 5;
15     private static int maxCount = 10;
16     private int currentCount = 0;
17 
18     private LinkedList<Connection> connectionPool = new LinkedList<Connection>();
19 
20     public MyDataSource() {
21         try {
22             for (int i = 0; i < initCount; i++) {
23 
24                 this.connectionPool.addLast(this.createConnection());
25 
26                 this.currentCount++;
27             }
28         } catch (SQLException e) {
29             throw new ExceptionInInitializerError(e);
30         }
31 
32     }
33 
34     public Connection getConnection() throws SQLException {
35         synchronized (connectionPool) {
36             if (this.connectionPool.size() > 0) {
37                 return this.connectionPool.removeFirst();
38             }
39             if (this.currentCount < maxCount) {
40                 this.currentCount++;
41                 return this.createConnection();
42             }
43             throw new SQLException("NO Connection!!");
44         }
45 
46     }
47 
48     public void free(Connection conn) {
49         this.connectionPool.addLast(conn);
50     }
51 
52     private Connection createConnection() throws SQLException {
53         return DriverManager.getConnection(url, username, password);
54     }
55 }
MyDataSource
bubuko.com,布布扣
  1 package it.cast.jdbc;
  2 
  3 import java.sql.CallableStatement;
  4 import java.sql.Connection;
  5 import java.sql.DriverManager;
  6 import java.sql.PreparedStatement;
  7 import java.sql.ResultSet;
  8 import java.sql.SQLException;
  9 import java.sql.Statement;
 10 
 11 public class jdbcUtils {
 12 
 13     private static String url = "jdbc:mysql://localhost:3306/jdbc?generateSimpleParameterMetadata=true";
 14     private static String user = "root";
 15     private static String password = "123";
 16     
 17     private static MyDataSource myDataSource = null;
 18 
 19     private jdbcUtils() {
 20 
 21     }
 22 
 23     static {
 24         try {
 25             Class.forName("com.mysql.jdbc.Driver");
 26             myDataSource =new MyDataSource();
 27         } catch (ClassNotFoundException e) {
 28             e.printStackTrace();
 29         }
 30     }
 31 
 32     public static Connection getConnection() throws SQLException {
 33         return myDataSource.getConnection();
 34     }
 35 
 36     public static void free(ResultSet rs, Statement st, Connection conn) {
 37 
 38         try {
 39             if (rs != null)
 40                 rs.close();
 41         } catch (SQLException e) {
 42             e.printStackTrace();
 43         } finally {
 44 
 45             try {
 46                 if (st != null)
 47                     st.close();
 48             } catch (SQLException e) {
 49                 e.printStackTrace();
 50             } finally {
 51 
 52                 try {
 53                     if (conn != null)
 54                         //conn.close();
 55                         myDataSource.free(conn);
 56                 } catch (Exception e) {
 57                     e.printStackTrace();
 58                 }
 59             }
 60 
 61         }
 62 
 63     }
 64     
 65     public static void free(ResultSet rs, PreparedStatement ps, Connection conn) {
 66 
 67         try {
 68             if (rs != null)
 69                 rs.close();
 70         } catch (SQLException e) {
 71             e.printStackTrace();
 72         } finally {
 73 
 74             try {
 75                 if (ps != null)
 76                     ps.close();
 77             } catch (SQLException e) {
 78                 e.printStackTrace();
 79             } finally {
 80 
 81                 try {
 82                     if (conn != null)
 83                         //conn.close();
 84                         myDataSource.free(conn);
 85                 } catch (Exception e) {
 86                     e.printStackTrace();
 87                 }
 88             }
 89 
 90         }
 91 
 92     }
 93     public static void free(ResultSet rs, CallableStatement cs, Connection conn) {
 94 
 95         try {
 96             if (rs != null)
 97                 rs.close();
 98         } catch (SQLException e) {
 99             e.printStackTrace();
100         } finally {
101 
102             try {
103                 if (cs != null)
104                     cs.close();
105             } catch (SQLException e) {
106                 e.printStackTrace();
107             } finally {
108 
109                 try {
110                     if (conn != null)
111                         //conn.close();
112                         myDataSource.free(conn);
113                 } catch (Exception e) {
114                     e.printStackTrace();
115                 }
116             }
117 
118         }
119 
120     }
121 }
jdbcUtils
bubuko.com,布布扣
 1 package it.cast.jdbc;
 2 
 3 import java.sql.Connection;
 4 import java.sql.ResultSet;
 5 import java.sql.SQLException;
 6 import java.sql.Statement;
 7 
 8 public class Base {
 9     
10     static Connection conn = null;
11 
12     public static void main(String[] args) throws SQLException, ClassNotFoundException {
13         for (int i = 0; i < 20; i++) {
14             Connection conn = jdbcUtils.getConnection();
15             System.out.println(conn);
16             //jdbcUtils.free(null, null, conn);
17         }
18     }
19 
20     static void test() throws SQLException, ClassNotFoundException {
21         
22 
23         // 2.建立连接
24         conn = jdbcUtils.getConnection();
25 
26         // 3.创建语句
27         Statement st = conn.createStatement();
28 
29         // 4.执行语句
30         ResultSet rs = st.executeQuery("select * from user");
31 
32         // 5.处理结果
33         while (rs.next()) {
34             System.out.println(rs.getObject(1) + "\t" + rs.getObject(2) + "\t"
35                     + rs.getObject(3)+"\t" + rs.getObject(4));
36         }
37         
38         //6.释放资源
39         jdbcUtils.free(rs, st, conn);
40     }
41 
42 }
Base

 

编写一个基本的连接池来实现连接的复用&一些工程细节上的优化

原文:http://www.cnblogs.com/aineko/p/3954989.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!