Home  >  Article  >  Database  >  Analysis of MySQL connection pool built-in jdbc

Analysis of MySQL connection pool built-in jdbc

王林
王林forward
2023-05-29 15:40:141151browse

Introduction

The following is com.mysql.cj.jdbc.MysqlConnectionPoolDataSource usage practice, which is relatively simple. There are quite a few APIs, but most of them are not used.

package com.funtest.groovytest
import com.funtester.frame.SourceCode
import com.mysql.cj.jdbc.MysqlConnectionPoolDataSource
class MysqlPoolTe extends SourceCode {
    public static void main(String[] args) {
        def query = "select * from testers limit 2;"
        def source = new MysqlConnectionPoolDataSource()
        source.setServerName("localhost")
        source.setPort(3306)
        source.setUser("root")
        source.setPassword("root123456")
        source.setDatabaseName("funtester")
        source.setAllowMultiQueries(true)
        def connection = source.getPooledConnection()
        def statement = connection.getConnection().createStatement()
        while (true) {
            sleep(1)
            def query = statement.executeQuery(query)
            while (query.next()) {
                output query.getString("name")
            }
        }
    }
}

There is a very easy pitfall here, that is, there is a setURL() and a setUrl(). In fact, there is no difference between the two. I just I can say that it may be for compatibility with older versions. Also, after setting the URL, it seems that the database setting does not work. Miao Ming feels that the design is really bad, so I did not use these two methods in the above case.

I tested and found that although I created a large number of threads, only a few were always connected. It will probably be recycled after a few seconds, but the total number of creations is still very high.

Analysis of MySQL connection pool built-in jdbc

The above is the detailed content of Analysis of MySQL connection pool built-in jdbc. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete