# 错误：sql：从连接池返回的连接已被服务器关闭

- **ID:** `go/database-sql-conn-max-lifetime-exceeded`
- **领域:** go
- **类别:** config_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

数据库服务器因空闲超时关闭了连接，但连接池仍将其视为有效并返回。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 1.20 | active | — | — |
| 1.21 | active | — | — |

## 解决方案

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

## 无效尝试

- **Setting ConnMaxLifetime too high** — Long-lived connections may be closed by server anyway; need to match server timeout. (60% 失败率)
