go runtime_error ai_generated true

sql: 数据库已关闭

sql: database is closed

ID: go/database-sql-max-open-connections

其他格式: JSON · Markdown 中文 · English
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.

generic

解决方案

  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") }

无效尝试

常见但无效的做法:

  1. 50% 失败

    May be impossible if the database is down or configuration changed.

  2. 90% 失败

    All subsequent queries will fail, causing application malfunction.