Analyzing the Cost of Establishing MySQL Database Connections in Java
This article investigates how much time a simple Java program spends establishing and closing a MySQL connection, measures the latency of each TCP and MySQL handshake step with Wireshark, and demonstrates why connection pooling is essential for high‑traffic web services.
Background: The author, a senior architect, wants to explore the details and cost of creating a database connection in a web application, especially the overhead of opening a new connection for each request.
Analysis: Using a minimal Java example that loads the MySQL driver, sets the credentials, and calls DriverManager.getConnection , the author captures the entire handshake with Wireshark. The MySQL protocol runs over TCP, performing a three‑way handshake, a server handshake packet, and a client authentication packet, followed by several configuration exchanges before the connection is ready for queries.
Code example (initial connection): 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 forcibly closed
Wireshark analysis shows the TCP three‑way handshake and the MySQL binary protocol exchange. The measured time from the start of the handshake to the forced termination (excluding the final RST packet) is about 0.225 seconds (225 ms).
Measured latency (connection only): 10.416042 - 10.190799 = 0.225243s = 225.243ms
When the connection is properly closed with conn.close() , the total time drops to about 0.183 seconds (183 ms): 747.284311 - 747.100954 = 0.183357s = 183.357ms
Using these figures, the author estimates the daily cost for a site with 20 000 daily active users, each sending 5 requests (100 000 requests per day). Assuming a conservative 150 ms per connection, the total time spent only on establishing connections would be 15 000 000 ms ≈ 4.17 hours per day, highlighting the inefficiency of opening a new connection for every request.
Conclusion: Database connections are expensive; therefore, connection pooling is mandatory. Additional optimizations such as caching, prepared statements, and load balancing are also recommended.
Code example (proper 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();
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
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.