Analyzing MySQL Connection Latency in Java Web Applications
This article investigates the detailed steps and time consumption of establishing and closing a MySQL connection from a Java web application, using Wireshark packet captures and code examples to demonstrate why connection pooling is essential for high‑traffic services.
The article explores the overhead of creating a MySQL connection in a Java web application, aiming to quantify the time spent in each phase of the TCP‑based MySQL handshake and authentication process.
It begins with a minimal Java snippet that loads the MySQL driver and opens a 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);
// program ends, connection is forcibly closedUsing Wireshark, the author captures the full handshake, which consists of three main steps:
Step 1: Establish a TCP connection via the three‑way handshake.
Step 2: Server sends a handshake packet; the client responds.
Step 3: Client sends an authentication packet; upon successful verification the server returns an OK packet and the session is ready for commands.
The capture shows that the entire connection establishment takes approximately 225 ms (10.416042 s – 10.190799 s), which includes at least seven round‑trips between client and server.
When the connection is properly closed with Connection.close() , the shutdown sequence is captured as a four‑step TCP termination, taking about 183 ms:
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();Extrapolating these numbers to a site with 100 000 requests per day (assuming 150 ms per connection) shows that merely establishing connections would consume roughly 4 hours of CPU time daily, highlighting the critical need for a connection pool.
The article concludes that database connections are expensive, and recommends using connection pooling together with caching, SQL pre‑compilation, and load balancing to mitigate the performance impact.
Top Architecture Tech Stack
Sharing Java and Python tech insights, with occasional practical development tool tips.
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.