本文共 12742 字,大约阅读时间需要 42 分钟。
[代码] 连接池类
001 | package com.sli.util; |
003 | import java.io.FileInputStream; |
004 | import java.sql.Connection; |
005 | import java.sql.DriverManager; |
006 | import java.sql.SQLException; |
007 | import java.util.Enumeration; |
008 | import java.util.HashMap; |
009 | import java.util.LinkedList; |
010 | import java.util.Map; |
011 | import java.util.Properties; |
012 | import java.util.Timer; |
013 | import java.util.TimerTask; |
015 | public class DataSourcePool { |
017 | static int connectionCurrLink = 0; |
018 | static Map<String, String> map = null; |
019 | private static LinkedList<Connection> datasourcePool = newLinkedList<Connection>(); |
020 | // 通过静态代码块注册数据库驱动,保证注册只执行一次 |
022 | map = new HashMap<String, String>(); |
023 | Properties p = new Properties(); |
025 | p.loadFromXML(newFileInputStream("src\\DataSourcePool.xml")); |
026 | Enumeration<Object> dataSourceSet = p.keys(); |
027 | while (dataSourceSet.hasMoreElements()) { |
028 | String key = (String) dataSourceSet.nextElement(); |
029 | map.put(key, p.getProperty(key)); |
031 | Class.forName(map.get("conectionDriver")); |
032 | } catch (Exception e) { |
037 | public DataSourcePool() throws Exception { |
039 | // 通过构造函数启动定时器以达到定时释放空闲连接目的 |
040 | Timer timer = new Timer(); |
041 | timer.schedule(new TimerTask() { |
045 | // 得到空闲连接,datasourcePool里面有几个对象就表示有几个空闲连接 |
046 | int leisureLink = DataSourcePool.datasourcePool.size(); |
047 | System.out.println(leisureLink); |
049 | int connectionMinLink = Integer.parseInt(DataSourcePool.map |
050 | .get("connectionMinLink")); |
051 | // 当空闲连接大于DataSourcePool设置的最小连接数时则关闭 |
052 | if (leisureLink > connectionMinLink) { |
053 | for (int i = 0; i < leisureLink - connectionMinLink; i++) { |
054 | DataSourcePool.closeConnection(DataSourcePool.getConnection()); |
055 | connectionCurrLink--; |
058 | System.out.println("保持最小连接,将继续保持连接池"); |
060 | } catch (NumberFormatException e) { |
061 | throw new NumberFormatException("设置了无效的最小连接数"); |
062 | } catch (SQLException e) { |
064 | } catch (Exception e) { |
068 | }, 0, Long.parseLong(map.get("connectionTimer"))); |
073 | private static void createConnection(int type) throws Exception { |
078 | link = Integer.parseInt(map.get("connectionMinLink")); |
081 | //如果当前连接+增长连接大于设定的最大连接数时,将使用最大连接数-当前连接的数量。以保持平衡 |
082 | link = Integer.parseInt(map.get("connectionIncreaseLink")); |
083 | int maxLink = Integer.parseInt(map.get("conectionMaxLink")); |
084 | if (link + connectionCurrLink > maxLink) { |
085 | link = maxLink - connectionCurrLink; |
089 | for (int i = 0; i < link; i++) { |
090 | datasourcePool.addLast(DriverManager.getConnection(map.get("connectionUrl"), |
091 | map.get("connectionName"), map.get("connectionPassword"))); |
092 | connectionCurrLink++; |
094 | } catch (NumberFormatException n) { |
095 | throw new NumberFormatException("配置连接参数有误"); |
096 | } catch (Exception e) { |
098 | throw new SQLException("超过最大连接数 ,无法创建更多连接"); |
103 | public static Connection getConnection() throws Exception { |
105 | synchronized (datasourcePool) { |
106 | if (datasourcePool.size() > 0) { |
107 | return datasourcePool.removeFirst(); |
108 | } else if (connectionCurrLink < Integer.parseInt(map.get("conectionMaxLink"))) { |
110 | return datasourcePool.removeFirst(); |
122 | public static void closeConnection(Connection con) throwsSQLException { |
127 | public void freeConnection(Connection con) { |
128 | datasourcePool.addLast(con); |
[代码] jdbcUtil作为封装连接池的辅助类
03 | import java.sql.Connection; |
04 | import java.sql.ResultSet; |
05 | import java.sql.SQLException; |
06 | import java.sql.Statement; |
12 | public final class JDBCUtil { |
13 | private static DataSourcePool dsp = null; |
15 | public JDBCUtil() throws Exception { |
24 | public static Connection getConnection( ) throws Exception { |
26 | synchronized (DataSourcePool.class) { |
28 | dsp = new DataSourcePool(); |
32 | return dsp.getConnection(); |
36 | public static void freeConnection(ResultSet rs, Statement ps, Connection con) |
42 | } catch (SQLException e) { |
49 | } catch (Exception e) { |
53 | dsp.freeConnection(con); |
59 | public static void freeConnection(Connection con) |
62 | dsp.freeConnection(con); |
[代码] 测试类
03 | import java.sql.Connection; |
05 | import com.sli.util.JDBCUtil; |
07 | public class TestDataPool { |
08 | public static void main(String[] args) throws Exception { |
09 | long start = System.currentTimeMillis(); |
11 | System.out.println("连接池开始:" + start); |
12 | for (int i = 0; i <20; i++) { |
13 | Connection con = JDBCUtil.getConnection(); |
14 | System.out.println(con); |
16 | JDBCUtil.freeConnection(con); |
19 | long end = System.currentTimeMillis(); |
20 | System.out.println("连接池结束:" + end); |
21 | System.out.println("连接池耗时:" + (end - start)); |
[代码] 配置连接xml
01 | <?xml version="1.0" encoding="utf-8"?> |
02 | <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"> |
05 | <entry key="connectionUrl">jdbc:mysql:///test</entry> |
07 | <entry key="connectionName">root</entry> |
09 | <entry key="connectionPassword">sli</entry> |
10 | <!-- 最小连接数(首次以此值为创建连接数标准及空闲连接大于该数时则会清除) --> |
11 | <entry key="connectionMinLink">5</entry> |
13 | <entry key="conectionMaxLink">89</entry> |
15 | <entry key="conectionDriver">com.mysql.jdbc.Driver</entry> |
17 | <entry key="connectionIncreaseLink">10</entry> |
18 | <!-- 设定自动清除空闲连接时间(毫秒)--> |
19 | <entry key="connectionTimer">5000</entry> |
20 | </properties> 代码片段(4) [代码] 连接池类 001 | package com.sli.util; | 003 | import java.io.FileInputStream; | 004 | import java.sql.Connection; | 005 | import java.sql.DriverManager; | 006 | import java.sql.SQLException; | 007 | import java.util.Enumeration; | 008 | import java.util.HashMap; | 009 | import java.util.LinkedList; | 010 | import java.util.Map; | 011 | import java.util.Properties; | 012 | import java.util.Timer; | 013 | import java.util.TimerTask; | 015 | public class DataSourcePool { | 017 | static int connectionCurrLink = 0; | 018 | static Map<String, String> map = null; | 019 | private static LinkedList<Connection> datasourcePool = newLinkedList<Connection>(); | 020 | // 通过静态代码块注册数据库驱动,保证注册只执行一次 | 022 | map = new HashMap<String, String>(); | 023 | Properties p = new Properties(); | 025 | p.loadFromXML(newFileInputStream("src\\DataSourcePool.xml")); | 026 | Enumeration<Object> dataSourceSet = p.keys(); | 027 | while (dataSourceSet.hasMoreElements()) { | 028 | String key = (String) dataSourceSet.nextElement(); | 029 | map.put(key, p.getProperty(key)); | 031 | Class.forName(map.get("conectionDriver")); | 032 | } catch (Exception e) { | 037 | public DataSourcePool() throws Exception { | 039 | // 通过构造函数启动定时器以达到定时释放空闲连接目的 | 040 | Timer timer = new Timer(); | 041 | timer.schedule(new TimerTask() { | 045 | // 得到空闲连接,datasourcePool里面有几个对象就表示有几个空闲连接 | 046 | int leisureLink = DataSourcePool.datasourcePool.size(); | 047 | System.out.println(leisureLink); | 049 | int connectionMinLink = Integer.parseInt(DataSourcePool.map | 050 | .get("connectionMinLink")); | 051 | // 当空闲连接大于DataSourcePool设置的最小连接数时则关闭 | 052 | if (leisureLink > connectionMinLink) { | 053 | for (int i = 0; i < leisureLink - connectionMinLink; i++) { | 054 | DataSourcePool.closeConnection(DataSourcePool.getConnection()); | 055 | connectionCurrLink--; | 058 | System.out.println("保持最小连接,将继续保持连接池"); | 060 | } catch (NumberFormatException e) { | 061 | throw new NumberFormatException("设置了无效的最小连接数"); | 062 | } catch (SQLException e) { | 064 | } catch (Exception e) { | 068 | }, 0, Long.parseLong(map.get("connectionTimer"))); | 073 | private static void createConnection(int type) throws Exception { | 078 | link = Integer.parseInt(map.get("connectionMinLink")); | 081 | //如果当前连接+增长连接大于设定的最大连接数时,将使用最大连接数-当前连接的数量。以保持平衡 | 082 | link = Integer.parseInt(map.get("connectionIncreaseLink")); | 083 | int maxLink = Integer.parseInt(map.get("conectionMaxLink")); | 084 | if (link + connectionCurrLink > maxLink) { | 085 | link = maxLink - connectionCurrLink; | 089 | for (int i = 0; i < link; i++) { | 090 | datasourcePool.addLast(DriverManager.getConnection(map.get("connectionUrl"), | 091 | map.get("connectionName"), map.get("connectionPassword"))); | 092 | connectionCurrLink++; | 094 | } catch (NumberFormatException n) { | 095 | throw new NumberFormatException("配置连接参数有误"); | 096 | } catch (Exception e) { | 098 | throw new SQLException("超过最大连接数 ,无法创建更多连接"); | 103 | public static Connection getConnection() throws Exception { | 105 | synchronized (datasourcePool) { | 106 | if (datasourcePool.size() > 0) { | 107 | return datasourcePool.removeFirst(); | 108 | } else if (connectionCurrLink < Integer.parseInt(map.get("conectionMaxLink"))) { | 110 | return datasourcePool.removeFirst(); | 122 | public static void closeConnection(Connection con) throwsSQLException { | 127 | public void freeConnection(Connection con) { | 128 | datasourcePool.addLast(con); | [代码] jdbcUtil作为封装连接池的辅助类 03 | import java.sql.Connection; | 04 | import java.sql.ResultSet; | 05 | import java.sql.SQLException; | 06 | import java.sql.Statement; | 12 | public final class JDBCUtil { | 13 | private static DataSourcePool dsp = null; | 15 | public JDBCUtil() throws Exception { | 24 | public static Connection getConnection( ) throws Exception { | 26 | synchronized (DataSourcePool.class) { | 28 | dsp = new DataSourcePool(); | 32 | return dsp.getConnection(); | 36 | public static void freeConnection(ResultSet rs, Statement ps, Connection con) | 42 | } catch (SQLException e) { | 49 | } catch (Exception e) { | 53 | dsp.freeConnection(con); | 59 | public static void freeConnection(Connection con) | 62 | dsp.freeConnection(con); | [代码] 测试类 03 | import java.sql.Connection; | 05 | import com.sli.util.JDBCUtil; | 07 | public class TestDataPool { | 08 | public static void main(String[] args) throws Exception { | 09 | long start = System.currentTimeMillis(); | 11 | System.out.println("连接池开始:" + start); | 12 | for (int i = 0; i <20; i++) { | 13 | Connection con = JDBCUtil.getConnection(); | 14 | System.out.println(con); | 16 | JDBCUtil.freeConnection(con); | 19 | long end = System.currentTimeMillis(); | 20 | System.out.println("连接池结束:" + end); | 21 | System.out.println("连接池耗时:" + (end - start)); | [代码] 配置连接xml 01 | <?xml version="1.0" encoding="utf-8"?> | 02 | <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"> | 05 | <entry key="connectionUrl">jdbc:mysql:///test</entry> | 07 | <entry key="connectionName">root</entry> | 09 | <entry key="connectionPassword">sli</entry> | 10 | <!-- 最小连接数(首次以此值为创建连接数标准及空闲连接大于该数时则会清除) --> | 11 | <entry key="connectionMinLink">5</entry> | 13 | <entry key="conectionMaxLink">89</entry> | 15 | <entry key="conectionDriver">com.mysql.jdbc.Driver</entry> | 17 | <entry key="connectionIncreaseLink">10</entry> | 18 | <!-- 设定自动清除空闲连接时间(毫秒)--> | 19 | <entry key="connectionTimer">5000</entry> | |
转载于:https://my.oschina.net/lvzjane/blog/103810