Databases 5 min read

Why MySQL’s MysqlConnectionPoolDataSource Isn’t a Real Connection Pool (And How to Use It)

The article examines the MySQL class com.mysql.cj.jdbc.MysqlConnectionPoolDataSource, revealing that despite its name it creates new connections each request and only recycles idle ones, provides a practical Groovy example, highlights pitfalls such as duplicate setURL methods, and concludes it’s unsuitable as a true pool for testing.

FunTester
FunTester
FunTester
Why MySQL’s MysqlConnectionPoolDataSource Isn’t a Real Connection Pool (And How to Use It)

In a previous post the author built a custom MySQL connection pool using Apache Commons‑Pool2 and modeled the API after GORM, hiding borrow and return operations from users.

While recording a video, IntelliJ suggested the class com.mysql.cj.jdbc.MysqlConnectionPoolDataSource, which seemed to match the desired functionality.

Documentation for this class is sparse and the official MySQL docs give the impression that it is not the recommended way to manage connections, especially in a Spring environment where more robust pooling solutions exist. However, for lightweight, script‑driven testing the author wanted to evaluate it.

What the class actually does

The name is misleading: the implementation does not pool connections for reuse. Each call to obtain a connection creates a new physical connection, and idle connections are simply closed after a configurable timeout via the close() method. This behaviour defeats the primary goal of a connection pool, which is to reduce the overhead of repeatedly opening and closing connections.

Simple Groovy usage example

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 result = statement.executeQuery(query)
            while (result.next()) {
                output result.getString("name")
            }
        }
    }
}

A common pitfall is the presence of both setURL() and setUrl() methods, which are functionally identical and exist for backward compatibility. After setting the URL, the setDatabaseName() call may have no effect, so the author avoided using these methods in the example.

Performance test observations

The author ran a test that spawned many threads. Although thousands of threads were created, only a few connections were active at any time; connections were reclaimed after a few seconds, leading to a high rate of connection creation.

Based on these findings, the author decided not to adopt com.mysql.cj.jdbc.MysqlConnectionPoolDataSource as a true pool and will continue improving the custom pool implementation.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavadatabasePerformance TestingConnection PoolmysqlGroovy
FunTester
Written by

FunTester

10k followers, 1k articles | completely useless

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.