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

Analysis of MySQL connection pool built-in jdbc

王林
Release: 2023-05-29 15:40:14
forward
1191 people have browsed it

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")
            }
        }
    }
}
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template