# sql：连接池耗尽：打开连接过多（max_open_conns=10）

- **ID:** `go/sql-too-many-open-connections`
- **领域:** go
- **类别:** resource_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

处理程序获取连接后未 Close rows 或归还连接，或 MaxOpenConns 对并发量设置过低。未关闭的 *sql.Rows 会耗尽连接池。

## 版本兼容性

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

## 解决方案

1. **** (92% 成功率)
   ```
   rows, err := db.QueryContext(ctx, q)
if err != nil { return err }
defer rows.Close()
for rows.Next() { ... }
return rows.Err()
   ```
2. **** (85% 成功率)
   ```
   db.SetMaxOpenConns(50)
db.SetMaxIdleConns(25)
db.SetConnMaxLifetime(5*time.Minute)
   ```

## 无效尝试

- **** — Hides the leak and can exhaust the database server's connection limit. (80% 失败率)
- **** — Does not release leaked connections; just delays the failure. (90% 失败率)
