golang sql database connection pool timeout is automatically disconnected -> 127.0.0.1:3 306: wsarecv: An established connection was aborted by the software in your host machine.

If mysql database, the database itself has a timeout, over this time, the database will automatically disconnect

View database disconnect time

show global variables like "%timeout%"

Mainly to see the value of interactive_timeout and wait_timeout. (Interactive_timeout for interactive connection, wait_timeout. Interactive Connectivity for so-called non-interactive connection, namely () function used in CLIENT_INTERACTIVE option mysql_real_connect. Put it bluntly, the mysql client database via an interactive connection through jdbc connected non-interactive database connection when the connection starts, depending on the type of connection, to confirm the value of the session variable is inherited wait_timeout wait_timeout global variables, or interactive_timeout seconds)

SetMaxOpenConns for setting the maximum number of open connections, the default value of 0 indicates no limit.
SetMaxIdleConns idle setting for the number of connections.
SetConnMaxLifetime (time.Duration (8 * 3600) * time.Second) // Set the timeout (default is not set to permanent)

Therein lies the problem: If sqlx connection pool for a long time without a database connection, the database is automatically disconnected, this time will result in a connection error

[mysql] 2019/12/25 20:04:44 packets.go:36: read tcp 127.0.0.1:60160->127.0.0.1:3
306: wsarecv: An established connection was aborted by the software in your host
machine.

Solution 2:

1 Before the database connection failure automatically connected to a database (not recommended).

2 Set SetConnMaxLifetime expiration time, and the time to failure is consistent to the database SetConnMaxLifetime (time.Duration (8 * 3600) * time.Second)

 

Guess you like

Origin www.cnblogs.com/hailong88/p/12098810.html