美文网首页
Mysql连接池具体实现

Mysql连接池具体实现

作者: tommy_yang | 来源:发表于2018-12-03 22:40 被阅读74次

Mysql连接池

就是存储一推mysql连接的尺子。

现在现成的Mysql连接池第三方jar应该有很多,但是有时候我们在项目中只是需要简单的用一下连接池去跑大量数据,比如在跑batch任务的时候,可能就不想去引入第三方jar包,于是我自己去简单了实现了一下连接池。

其实连接池也很简单,就是我们提前申请好与mysql的connection连接,然后讲这些连接放入到内存里面,这样我们的任务需要连接mysql数据库的时候,就可以从我们提前申请好的连接池中去获取连接,而不用每次都去与

mysql server连接,如果你在短时间内与mysql server频繁连接的话,可能会导致连接错误,所以我们应该避免这样的问题。

具体实现

首先我定义了一个DataSource类,这个类负责初始化mysql connection,以及将释放的connection存储起来。我这里使用LinkedList来作为存储的数据结构,因为我们会频繁的对这个线程池进行增加和删除

(一个线程来获取就要从池子里面删除一个connection,线程运行完就要将connection加入到池子),所以采用此结构;

下面讲一下代码的具体实现,实现一个具体的DataSource类:

DataSource类

    private final String url = ""; //mysql 连接串
    private final String user = ""; //用户名
    private final String password = ""; //密码

    private static int initCount = 10; //初始化connection个数
    private static int maxCount = 20; //connection最大个数
    private static int currentCount = 0;

    private LinkedList<Connection> connectionPool = new LinkedList<>();

    //只让包内引用,下面会定义一个JDBCUtils去获取Connection连接
    protected MyDataSource() {

        for(int i = 0; i < initCount; i++){
            try {
                this.connectionPool.addLast(this.createConnection());
                this.currentCount++;
            }catch (Exception e){
                throw new ExceptionInInitializerError(e);
            }
        }

    }

    private Connection createConnection() throws SQLException{
        return DriverManager.getConnection(this.url, this.user, this.password);
    }

    private Connection createConnection() throws SQLException{
        return DriverManager.getConnection(this.url, this.user, this.password);
    }

    protected Connection getConnection() throws SQLException {
        synchronized(this.connectionPool){
            if(this.connectionPool.size() > 0){
                this.currentCount--;
                return this.connectionPool.removeFirst();
            }

            if(this.currentCount < this.maxCount){
                this.createConnection();
                this.currentCount++;
            }
            throw new SQLException("no connection");
        }
    }

    protected void free(Connection conn){
        synchronized (this.connectionPool){
            this.connectionPool.addLast(conn);
            this.currentCount++;
        }
    }

JDBCUtils具体实现类

在DataSource类的同包内定义该类,对外提供获取connection连接和释放Connection连接的工具类。

具体实现如下:

private static MyDataSource dataSource = null;
    private final static String driver = "";//jdbc driver name

    static {
        try {
            Class.forName(driver);
            dataSource = new MyDataSource();
        }catch (ClassNotFoundException e){
            throw new ExceptionInInitializerError(e);
        }
    }

    //对外提供的获取连接的方法
    public static Connection getConnection() throws SQLException {
        return dataSource.getConnection();
    }

    //对外提供的释放连接的方法
    public static void free(ResultSet rs, Statement st, Connection conn) throws SQLException {
        if(rs != null){
            rs.close();
        }

        if(st != null){
            st.close();
        }

        dataSource.free(conn);
    }

总结

自己去实现一个连接池类,方便自己去理解连接池的概念;有任何问题,可以在评论区进行讨论。

原博客地址

TommyYang's Blog

相关文章

  • Mysql连接池具体实现

    Mysql连接池 就是存储一推mysql连接的尺子。现在现成的Mysql连接池第三方jar应该有很多,但是有时候我...

  • Flink自定义MySQLSource读取MySQL数据

    先查看数据库连接池实现MySQL连接池 MySQL表student的实体采用case class定义 实现Rich...

  • 深入解析Node.js使用MySQL连接池

    这篇文章主要介绍了Node.js使用MySQL连接池的方法,结合具体实例形式分析了nodejs操作mysql连接池...

  • Python实现Mysql数据库连接池

    Python实现Mysql数据库连接池 python连接Mysql数据库: python编程中可以使用MySQLd...

  • gobox中的连接池pool

    今天来说下gobox中的连接池底层实现pool 为什么需要连接池 我们的系统在访问外部资源(redis、mysql...

  • Openresty Lua实现mysql连接池

    最近接触到了mysql数据库的连接池,做个记录使用openresty框架,实现语言为Lua 通过连接池获取连接 1...

  • MySQL线程池

    一、相关概念 MySQL连接池:连接池通常实现在Client端,是指应用(客户端)预先创建一定的连接,利用这些连接...

  • Django实现MySQL连接池

    注:笔者的Django版本为 1.9,Python版本为 2.7 一直想在Django中实现MySQL连接池,在网...

  • Node.js连接mysql (封装)

    下载mysql模块npm install mysql --save 创建mysql连接池(connect.js文件...

  • DBCP

    硬编码实现连接池 配置方式实现连接池 配置文件

网友评论

      本文标题:Mysql连接池具体实现

      本文链接:https://www.haomeiwen.com/subject/sufpcqtx.html