# 错误：sql：数据库已关闭

- **ID:** `go/database-sql-connection-bad-connection`
- **领域:** go
- **类别:** network_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

在数据库关闭后尝试使用数据库连接，通常由于过早调用db.Close()。

## 版本兼容性

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

## 解决方案

1. **Use a single db instance and close only when shutting down** (95% 成功率)
   ```
   db, err := sql.Open("driver", dsn)
if err != nil { log.Fatal(err) }
defer db.Close()
// use db for all queries
   ```

## 无效尝试

- **Reopening database for each query** — Inefficient; use connection pool and defer close at application shutdown. (70% 失败率)
