# sql: 连接池耗尽：连接过多

- **ID:** `go/sql-connection-pool-exhausted`
- **领域:** go
- **类别:** resource_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

并发数据库请求超过可用连接数，达到连接池最大限制。

## 版本兼容性

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

## 解决方案

1. **** (90% 成功率)
   ```
   Use connection pooling with SetMaxOpenConns and SetMaxIdleConns, and use context timeouts: db.SetMaxOpenConns(10); db.SetConnMaxLifetime(time.Minute)
   ```
2. **** (85% 成功率)
   ```
   Implement a semaphore to limit concurrent queries: sem := make(chan struct{}, 10); sem <- struct{}{}; defer func(){<-sem}()
   ```

## 无效尝试

- **** — Unlimited connections can overwhelm database server. (60% 失败率)
- **** — May not be feasible; pool still limited. (50% 失败率)
