# sql: 数据库已关闭

- **ID:** `go/database-sql-max-open-connections`
- **领域:** go
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

在调用 db.Close() 后尝试使用数据库句柄，导致错误。

## 版本兼容性

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

## 解决方案

1. **** (90% 成功率)
   ```
   Use a sync.Once to close the database only once: var closeOnce sync.Once; closeOnce.Do(func() { db.Close() })
   ```
2. **** (70% 成功率)
   ```
   Check if db is nil before using: if db == nil { return errors.New("db is closed") }
   ```

## 无效尝试

- **** — May be impossible if the database is down or configuration changed. (50% 失败率)
- **** — All subsequent queries will fail, causing application malfunction. (90% 失败率)
