自定义连接池

普通的数据库连接

连接资源宝贵,需要对连接进行管理,

操作数据:创建连接

操作结束:关闭连接

分析:然而在程序中频繁的打开和关闭连接会影响程序的运行的效率

自定义的数据库的连接池:

预先创建一个连接,用的时候取出一个,用完后放回。能够提高程序的运行效率。

  1 import java.sql.Connection;
  2 import java.sql.DriverManager;
  3 import java.sql.SQLException;
  4 import java.util.LinkedList;
  5 
  6 /**
  7  * 自定义连接池类
  8     1.    创建连接池类
  9     2.    指定一些全局参数,
 10     初始化连接数量,
 11     最大连接数,
 12     当前连接数,
 13     连接池集合(用来存放连接的)
 14     3.    构造函数,循环创建初始化连接数目,
 15     4.    写一个创建连接的方法,
 16     5.    写一个获取连接的方法:先判断连接池中是否有连接,如果有直接使用,如果没有连接,则先判断当前的连接是否达到最大连接数,达到的话 抛出异常,没有达到最大连接 则创建连接。
 17     6.    写一个释放连接的方法:把连接放回连接池(集合)中。如果池中连接数小初始化连接数目就放回池中,其他则关闭连接。
 18  * @author xinqi
 19  *
 20  */
 21 public class MyPool {
 22     
 23     //初始化连接数目
 24     private int initCount = 3;
 25     //最大连接数目
 26     private int initMaxCount = 6;
 27     //记录当前使用 的连接数目
 28     private int currentCount = 0;
 29     
 30     //连接池   使用linkedList集合存储   存放初始化的连接
 31     private LinkedList<Connection> connectionPool = new LinkedList<>();
 32     
 33     
 34     //构造函数  初始化连接
 35     public MyPool(){
 36         //初始化3个连接
 37         for(int i=0;i<3;i++){
 38             connectionPool.add(createConnection());
 39         }
 40     }
 41         
 42     /**
 43      * 创建连接的方法
 44      * @return  Connection
 45      */
 46     public    Connection createConnection(){
 47             
 48         try {
 49             Class.forName("com.mysql.jdbc.Driver");
 50             return DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");
 51         } catch (Exception e) {
 52             // TODO Auto-generated catch block
 53             e.printStackTrace();
 54         }
 55         return null;
 56     }
 57     
 58     /**
 59      * 获取连接的方法
 60      * @return
 61      */
 62     public Connection getConnection(){
 63         
 64         //1.判断,池中若有连接直接使用
 65         if(connectionPool.size()>0){
 66             //把这个链接移出集合并返回当前连接对象。
 67             currentCount++;
 68             return connectionPool.removeFirst();
 69             
 70         }
 71         //如果池中没有连接而且没有达到最大连接数目;则创建连接
 72         if(currentCount>=initCount && currentCount<initMaxCount){
 73             currentCount++;
 74             //创建一个新的连接
 75             return createConnection();
 76         }
 77         //判断是否达到最大连接数,达到则抛出异常
 78         throw new RuntimeException("当前连接已经达到最大连接数!");
 79     }
 80     
 81     
 82     /**
 83      * 释放连接的方法
 84      * @param conn
 85      */
 86     public void releaseConnection(Connection conn){
 87         //判断池中的数目如果小于初始化连接就放回连接池中。
 88         
 89         //判断连接池中的剩余数目是否<连接池初始化数目   如果为真 则放回连接池
 90         //
 91         
 92         if(currentCount<=initCount){
 93             //放回连接池
 94             connectionPool.addLast(conn);
 95             //当前连接-1
 96             currentCount--;
 97             
 98         }else{
 99             //关闭连接
100             try {
101                 conn.close();
102                 currentCount--;
103             } catch (SQLException e) {
104                 e.printStackTrace();
105             }
106         }
107     }
108     
109     
110     public static void main(String[] args) {
111         
112         MyPool pool = new MyPool();
113         
114         Connection conn1 = pool.getConnection();
115         Connection conn2 = pool.getConnection();
116         Connection conn3 = pool.getConnection();
117         Connection conn4 = pool.getConnection();
118         
119         
120         System.out.println("池中初始化的连接:"+pool.initCount);
121         System.out.println("最大连接数目:"+pool.initMaxCount);
122         
123         System.out.println("池中剩余连接:"+pool.connectionPool.size());
124         System.out.println("当前连接数目:"+pool.currentCount);
125         
126     }
127 }
原文地址:https://www.cnblogs.com/panxinqi/p/7041017.html