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

其他格式: JSON · Markdown 中文 · English
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.

generic

解决方案

  1. 90% 成功率 Set ConnMaxLifetime to less than server's wait_timeout
    db.SetConnMaxLifetime(5 * time.Minute)
    db.SetMaxIdleConns(10)

无效尝试

常见但无效的做法:

  1. Setting ConnMaxLifetime too high 60% 失败

    Long-lived connections may be closed by server anyway; need to match server timeout.