简易版JDBC连接池

JDBC连接池mini版的实现

首先是工具类 DbUtil

  • 主要参数就是Driver、User、PWD等啦,主要用于建立连接
  • URL需要注意的是SSL和serverTimezone参数,和mysql驱动版本有关
public class DbUtil {
    private static final String DRIVER = "com.mysql.cj.jdbc.Driver";
    private static final String URL = "jdbc:mysql://127.0.0.1:3306/school?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC";
    private static final String USER = "root";
    private static final String PWD = "123456";

    public static Connection getConnection(){

        try {
            Class.forName(DRIVER);
            Connection conn = (Connection) DriverManager.getConnection(URL,USER,PWD);
            System.out.println("generate a new connection:"+conn.hashCode());
            return conn;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}

DBPool连接池

  • 设置的一个最大容量为20的连接池
  • 使用静态代码块进行初始化,初始化用到的锁会在后面继续介绍
  • 主要包含三个方法,建立连接、扩充连接池和回收连接

public class DBPool {
    private static final int max = 20;
    private static final int min = 5;
    private static int poolSize = 0;

    private DBPool(){}

    private static ConcurrentLinkedQueue<Connection> pool = new ConcurrentLinkedQueue<Connection>();

    static {
        if (poolSize == 0 && pool.isEmpty()){
            synchronized (DBPool.class){
                if (poolSize == 0 && pool.isEmpty()){
                    try {
                        System.out.println("连接池为空,正在初始化");
                        for (int i = 0; i < min; i++) {
                            pool.add(DbUtil.getConnection());
                            poolSize++;
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    public static Connection getConnection(){
        try {
            if (!pool.isEmpty()){
                System.out.println("获得连接");
                return pool.remove();
            }else {
                System.out.println("正在扩大连接池");
                dilatation(5);
                System.out.println("正在获得连接");
                while (true){
                    if (!pool.isEmpty()){
                        System.out.println("获得连接");
                        return pool.remove();
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    public static void dilatation(int num){
        if (poolSize > max){
            System.out.println("超过连接池最大数量");
            return;
        }
        for (int i = 0; i < num; i++) {
            pool.add(DbUtil.getConnection());
            poolSize++;
        }
    }

    public static void close(Connection conn){
        pool.add(conn);
        System.out.println("回收成功");
    }
}

JDBCTest测试类


public class JDBCTest {
    public static void main(String[] args) {
        Connection c1 = DBPool.getConnection();
        DBPool.close(c1);
        System.out.println("该连接为:"+c1.hashCode());

       for (int i = 0; i < 20; i++) {
            System.out.println("连接"+i+" :"+DBPool.getConnection());
        }
    }
}
上一篇:Ubuntu 安装 NTP 服务


下一篇:神经网络更新覆盖的问题