go
runtime_error
ai_generated
true
sql: 数据库已关闭
sql: database is closed
ID: go/database-sql-max-open-connections
80%修复率
84%置信度
0证据数
2024-01-25首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 1.15 | active | — | — | — |
| 1.19 | active | — | — | — |
根因分析
在调用 db.Close() 后尝试使用数据库句柄,导致错误。
English
Attempting to use a database handle after calling db.Close(), resulting in an error.
解决方案
-
90% 成功率
Use a sync.Once to close the database only once: var closeOnce sync.Once; closeOnce.Do(func() { db.Close() }) -
70% 成功率
Check if db is nil before using: if db == nil { return errors.New("db is closed") }
无效尝试
常见但无效的做法:
-
50% 失败
May be impossible if the database is down or configuration changed.
-
90% 失败
All subsequent queries will fail, causing application malfunction.