Analyzing the Cost of Establishing MySQL Database Connections in Java Applications
This article examines the detailed steps and timing of establishing a MySQL database connection using Java, measures the latency of the TCP handshake and authentication phases, and demonstrates why connection pooling is essential for high‑traffic web applications.
Background: After developing applications for a long time, the author wants to dig into the details of database connections, especially the overhead of establishing a new connection for each request in a web application.
Analysis: The article uses MySQL as an example because its protocol is open, allowing a detailed breakdown of the connection process. The focus is on network latency, but memory and CPU usage are also mentioned. Java is the programming language used in the examples.
Below is the minimal Java code used to open a MySQL connection (5 lines):
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 closedThe connection establishment is captured with Wireshark, as shown in the following image:
The captured traffic reveals that MySQL uses a binary protocol over TCP. The connection process consists of the following steps:
Step 1: TCP three‑way handshake to establish the transport connection.
Step 2: Server sends a handshake packet; the client responds.
Step 3: Client sends an authentication packet; upon successful verification the server replies with OK and the session proceeds.
After authentication, the client and server exchange several configuration packets (character set, autocommit, etc.) before any SQL statements are executed.
Timing measurement for the connection (excluding the final TCP RST) shows:
10.416042 - 10.190799 = 0.225243s = 225.243msThis indicates that establishing a single MySQL connection takes about 225 ms in the minimal example.
When the connection is closed properly using conn.close() , the full lifecycle (including the TCP four‑way termination) takes:
747.284311 - 747.100954 = 0.183357s = 183.357msEven with a slightly lower figure, the cost remains around 200 ms per connection.
Implications: For a site with 20 000 daily active users, assuming each user generates 5 requests, there are 100 000 requests per day. At a conservative 150 ms per connection, the total time spent just establishing connections would be roughly 4.17 hours per day, underscoring the necessity of a connection pool.
Additional performance‑enhancing techniques include caching, SQL statement pre‑compilation, load balancing, and others.
Conclusion: Database connections are expensive; frequent creation of new connections should be avoided, and connection pooling is essential for scalable web applications.
Architecture Digest
Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.
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.