Analyzing MySQL Connection Latency in Java Applications
This article investigates the time cost of establishing and closing MySQL connections in Java web applications, using Wireshark packet captures and code examples to demonstrate that a single connection can take over 200 ms, highlighting the importance of connection pooling and performance optimizations.
The article explores the details of database connections in web applications, focusing on why establishing a connection is time‑consuming and how connection pooling can mitigate this overhead.
MySQL is chosen as the example because its open protocol allows a thorough analysis of the connection process.
The performance analysis concentrates on network resources, while also considering memory and CPU usage; the implementation language is Java.
Below is the Java code used to create a simple MySQL connection:
Class.forName("com.mysql.jdbc.Driver");
String name = "xttblog2";
String password = "123456";
String url = "jdbc:mysql://172.16.100.131:3306/xttblog2";
Connection conn = DriverManager.getConnection(url, name, password);
// 之后程序终止,连接被强制关闭Wireshark is then used to capture the entire connection establishment process, which consists of three main steps: (1) TCP three‑way handshake, (2) server sends handshake information and the client responds, (3) client sends an authentication packet, after which the server returns an OK response and the session proceeds.
After successful authentication, the client and server exchange several configuration packets (character set, autocommit, etc.) before any actual query execution.
In the test, the connection time measured (excluding the final TCP RST) was 0.225 s (225.243 ms), requiring at least seven round‑trips between client and server.
In a normal application the connection is closed explicitly with Connection.close() :
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 closing process, captured by Wireshark, shows the client sending a close request without waiting for a server response, followed by a TCP four‑way handshake; the total time for a full connect‑and‑close cycle was measured at 0.183 s (183.357 ms).
Assuming 100,000 requests per day with an average connection cost of 150 ms, the daily time spent just on establishing connections would be about 4.17 hours, underscoring the necessity of connection pooling and additional optimizations such as caching, SQL pre‑compilation, and load balancing.
The core message is that database connections are expensive and should not be created frequently.
Architect's Guide
Dedicated to sharing programmer-architect skills—Java backend, system, microservice, and distributed architectures—to help you become a senior architect.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.