Another night of working overtime, this article is going to get a makeover. But I found something funny today.

The background,

The database address used in JDBC connections is a domain name. When DNS switches IP or the local hosts changes IP, the connection pool will remain connected to the old library or will be updated to the new library.

Second, C3P0 test

As a result, the connection pool re-establishes a connection with the new IP address. If the domain name cannot resolve the IP address, there is no connection available in the connection pool.

netstat -ano | grep 3306
Copy the code

You will also notice that the connection status with the old IP address is slowly closed

Druid test

1, the local test conditions are limited, do not use two databases, through hosts to invalidate the domain name. JDBC connection JDBC :mysql://localhost-db:3306.

#hosts127.0.0.1 localhost - dbCopy the code

As a result, the connections in the connection pool are still valid and available. Using netstat on Linux – ano | grep, 3306 MAC using netstat – anvp TCP | grep, 3306.

Exactly five connections are the same size as the initial connection pool.

2, comment out

#hosts127.0.0.1 localhost - dbCopy the code

Druid can throw exceptions, solid printing error log com. Alibaba. The druid. Pool. DruidDataSource: create connection SQLException, url: JDBC: mysql: / / localhost – db: 3306, CreateConnectionThread# run

com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure The last packet sent successfully to the server was 0 milliseconds ago. The driver > has not received any packets from the server.

2, the domain IP added back almost immediately

In summary, the above shows that the database connection pool has a probe mechanism.

  1. C3p0 automatic detection, modify connection pool.

  2. Druid Deletes domain NAME IP addresses

The connection is still there, but when the access to connection query data interface is called, it is found that the network to the server is disconnected and all connections are destroyed.

3. Druid Adds the domain name back to the IP address configuration

Druid fires when a connection is acquired, and retries over and over in a loop. Host After the domain name is configured, establish a connection. C3p0 is the number of connections that can be restored to the specified size.