go
runtime_error
ai_generated
true
sql: 行已关闭
sql: Rows are closed
ID: go/database-sql-rows-closed
80%修复率
87%置信度
0证据数
2024-01-30首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 1.15 | active | — | — | — |
| 1.20 | active | — | — | — |
根因分析
在 sql.Rows 对象已关闭后(显式关闭或由于错误)尝试调用 Next() 或 Scan()。
English
Attempting to call Next() or Scan() on a sql.Rows object after it has been closed, either explicitly or due to an error.
解决方案
-
95% 成功率
Always check errors and call rows.Close() in defer: defer rows.Close(); for rows.Next() { ... } -
90% 成功率
Check rows.Err() after iteration: if err := rows.Err(); err != nil { handleErr(err) }
无效尝试
常见但无效的做法:
-
90% 失败
Rows are already closed; further calls will panic or return stale data.
-
60% 失败
May cause data inconsistency and resource leaks if not handled properly.