Databases 8 min read

Why a Single MySQL JDBC Connection Takes Over 200 ms – A Deep Dive

This article measures the time required to establish and close a MySQL JDBC connection, showing that a single connection can consume over 200 ms, and explains why connection pooling is essential for high‑traffic web applications.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Why a Single MySQL JDBC Connection Takes Over 200 ms – A Deep Dive

Background

Developers often wonder how much time is spent just creating a database connection in a web application. Although the answer is known to be “expensive”, the exact latency and its sources are rarely quantified.

Objective

The article investigates the cost of establishing a MySQL JDBC connection, using a minimal Java program and Wireshark packet captures to measure each step.

Test Code

Class.forName("com.mysql.jdbc.Driver");
String name = "shine_user";
String password = "123";
String url = "jdbc:mysql://172.16.100.131:3306/clever_mg_test";
Connection conn = DriverManager.getConnection(url, name, password);
// program ends, connection is abruptly closed

The program opens a connection and then terminates without calling Connection.close(), allowing the capture of the raw TCP teardown.

Wireshark Analysis – Connection Establishment

The capture reveals the MySQL protocol (binary over TCP) and the following sequence:

Step 1: TCP three‑way handshake.

Step 2: Server sends handshake packet; client replies.

Step 3: Client sends authentication packet; server returns OK and the session is ready for commands.

After authentication the client and server exchange several configuration packets (character set, autocommit, etc.).

Timing measured from the first SYN to the final ACK before the process exits: 10.416042 - 10.190799 = 0.225243s = 225.243ms Thus, establishing a single MySQL connection costs roughly 225 ms in this minimal scenario.

Graceful Close

When the code is amended to close the connection explicitly, the sequence changes:

Class.forName("com.mysql.jdbc.Driver");
String name = "shine_user";
String password = "123";
String url = "jdbc:mysql://172.16.100.131:3306/clever_mg_test";
Connection conn = DriverManager.getConnection(url, name, password);
conn.close();

The client now sends a MySQL‑level close request followed by a normal TCP four‑way termination.

Measured duration for the full open‑close cycle: 747.284311 - 747.100954 = 0.183357s = 183.357ms Even with a graceful shutdown the connection still requires about 180 ms.

Impact on a Real‑World Service

Assume a site with 20 000 daily active users, each issuing 5 requests (100 000 requests per day). If each request opened a new connection and the average cost is conservatively 150 ms, the daily time spent only on connection setup is:

100000 * 150ms = 15,000,000ms = 15,000s = 250min ≈ 4.2 hours

Four hours of CPU‑time are wasted solely on establishing connections, highlighting why connection pooling is indispensable.

Beyond Pooling

When traffic grows, pooling alone may not suffice. Additional techniques include:

Caching query results.

Using prepared statements to avoid repeated parsing.

Load balancing across multiple database instances.

Other optimizations such as read‑replicas or sharding.

Conclusion

Creating a MySQL JDBC connection is surprisingly costly—on the order of 200 ms. Applications that open a connection per request quickly waste valuable time, so a connection pool (or other latency‑reducing strategies) is essential for scalable web services.

Wireshark capture of MySQL connection establishment
Wireshark capture of MySQL connection establishment
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.

mysqlJDBCWiresharkConnection Pooling
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

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.