go
config_error
ai_generated
true
错误:sql:从连接池返回的连接已被服务器关闭
error: sql: connection returned from pool was closed by the server
ID: go/database-sql-conn-max-lifetime-exceeded
80%修复率
83%置信度
0证据数
2024-06-15首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 1.20 | active | — | — | — |
| 1.21 | active | — | — | — |
根因分析
数据库服务器因空闲超时关闭了连接,但连接池仍将其视为有效并返回。
English
The database server closed the connection due to idle timeout, but the connection pool returned it as still valid.
解决方案
-
90% 成功率 Set ConnMaxLifetime to less than server's wait_timeout
db.SetConnMaxLifetime(5 * time.Minute) db.SetMaxIdleConns(10)
无效尝试
常见但无效的做法:
-
Setting ConnMaxLifetime too high
60% 失败
Long-lived connections may be closed by server anyway; need to match server timeout.