How to Ensure Applications Using go-xorm Can Handle Disconnections and Automatically Reconnect to the TiDB Database When Disconnections Occur

Note:
This topic has been translated from a Chinese forum by GPT and might contain errors.

Original topic: 如何确保使用 go-xorm 的应用程序可以处理断开连接并在发生断开连接时自动重新连接到 TiDB 数据库

| username: ljluestc

[TiDB Usage Environment] Production Environment
[TiDB Version] v5.0.1
[Reproduction Steps]

  1. Connect to the TiDB database.
  2. Execute a series of query and update operations.
  3. Disconnect from the database.
  4. 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:

| username: ShawnYan | Original post link

Are you referring to this ORM?

I suggest checking their official documentation,

Adjust the parameters according to the situation.
Engine provides DB connections pool settings.

  • Use engine.SetMaxIdleConns() to set idle connections.
  • Use engine.SetMaxOpenConns() to set max connections. This method supports only Go 1.2+.
  • Use engine.SetConnMaxLifetime() to set max lifetime. This method supports only Go 1.6+.
| username: cassblanca | Original post link

You might also find surprises in the official documentation.

| username: redgame | Original post link

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.

| username: zhouzeru | Original post link

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.

| username: system | Original post link

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.