[TiDB Usage Environment] Production Environment
[TiDB Version] v5.0.1
[Reproduction Steps]
Connect to the TiDB database.
Execute a series of query and update operations.
Disconnect from the database.
Reconnect to the database after waiting for a period of time.
[Encountered Issue: Problem Phenomenon and Impact] During the steps in the reproduction path, the database connection was disconnected. This caused subsequent query and update operations to fail, impacting the functionality and performance of the application.
[Resource Configuration] TiDB Dashboard - Cluster Info - Hosts page screenshot as follows:
Set up a database connection pool: When creating the database engine, use the SetMaxIdleConns and SetMaxOpenConns methods to set the maximum number of idle connections and the maximum number of open connections in the connection pool to meet the application’s concurrency requirements.
During program execution, you can use the Ping() method of xorm.Engine to check the connection status. If the connection has been lost, you can restore it by reconnecting. You can use a scheduled task to periodically perform the Ping() operation to ensure the availability of the connection.