首页 > 技术文章 > 数据库的连接池和装饰者模式心得

yf8888888 2017-07-13 16:10 原文

 DataSource  比driverManager 获取Connection 速度快, 最重要的一点是

 close 方法 被修饰之后 不再是 “关连接”  而是  ”还链接“。

  1 package cn.tedu.jdbc.pool;
  2 
  3 import java.io.PrintWriter;
  4 import java.sql.Connection;
  5 import java.sql.DriverManager;
  6 import java.sql.SQLException;
  7 import java.sql.SQLFeatureNotSupportedException;
  8 import java.util.LinkedList;
  9 import java.util.List;
 10 import java.util.logging.Logger;
 11 
 12 import javax.sql.DataSource;
 13 
 14 public class MyPool implements DataSource{
 15     private static List<Connection> list = new LinkedList<Connection>();
 16     
 17     static{
 18         /* 初始化一批连接放在连接池中共享 */
 19         try {
 20             Class.forName("com.mysql.jdbc.Driver");
 21             for (int i = 0; i < 5; i++) {
 22                 Connection conn = DriverManager.getConnection("jdbc:mysql:///mydb1", "root", "root");
 23                 list.add(conn);
 24             }
 25             System.out.println("连接初始化成功....");
 26         } catch (Exception e) {
 27             e.printStackTrace();
 28             throw new RuntimeException(e);
 29         }
 30         
 31     }
 32     
 33     /**
 34      * 从连接池中获取一个连接并返回
 35      */
 36     public Connection getConnection() throws SQLException {
 37         //判断连接池是否还有连接
 38         if(list.isEmpty()){
 39             //如果连接池中的连接耗尽, 再次跟数据库要3个连接
 40             for (int i = 0; i < 3; i++) {
 41                 Connection conn = DriverManager.getConnection("jdbc:mysql:///mydb1", "root", "root");
 42                 list.add(conn);
 43             }
 44         }
 45         Connection conn = list.remove(0);
 46         //将连接对象包装后再返回出去
 47         Connection connDecorate = new ConnectionDecorate(conn, this);
 48         
 49         System.out.println("成功获取一个连接, 池中还剩"+list.size()+"个连接....");
 50         return connDecorate;
 51     }
 52     
 53     /**
 54      * 将连接对象还回连接池中!
 55      * @param conn
 56      */
 57     public void returnConn(Connection conn){
 58         try {
 59             if(conn != null && !conn.isClosed()){
 60                 list.add(conn);
 61                 System.out.println("成功还回一个连接, 池中还剩"+list.size()+"个连接....");
 62                 
 63             }
 64         } catch (Exception e) {
 65             e.printStackTrace();
 66             throw new RuntimeException(e);
 67         }
 68     }
 69 
 70     @Override
 71     public PrintWriter getLogWriter() throws SQLException {
 72         // TODO Auto-generated method stub
 73         return null;
 74     }
 75 
 76     @Override
 77     public void setLogWriter(PrintWriter out) throws SQLException {
 78         // TODO Auto-generated method stub
 79         
 80     }
 81 
 82     @Override
 83     public void setLoginTimeout(int seconds) throws SQLException {
 84         // TODO Auto-generated method stub
 85         
 86     }
 87 
 88     @Override
 89     public int getLoginTimeout() throws SQLException {
 90         // TODO Auto-generated method stub
 91         return 0;
 92     }
 93 
 94     @Override
 95     public Logger getParentLogger() throws SQLFeatureNotSupportedException {
 96         // TODO Auto-generated method stub
 97         return null;
 98     }
 99 
100     @Override
101     public <T> T unwrap(Class<T> iface) throws SQLException {
102         // TODO Auto-generated method stub
103         return null;
104     }
105 
106     @Override
107     public boolean isWrapperFor(Class<?> iface) throws SQLException {
108         // TODO Auto-generated method stub
109         return false;
110     }
111 
112     
113 
114     @Override
115     public Connection getConnection(String username, String password)
116             throws SQLException {
117         // TODO Auto-generated method stub
118         return null;
119     }
120         
121 }

 

推荐阅读