go
runtime_error
ai_generated
true
sql: 连接已被使用
sql: connection is already in use
ID: go/database-sql-conn-already-in-use
80%修复率
80%置信度
0证据数
2024-02-14首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 1.16 | active | — | — | — |
| 1.19 | active | — | — | — |
根因分析
数据库连接在仍被其他 goroutine 使用时被返回到池中,导致竞争条件。
English
A database connection was returned to the pool while still being used by another goroutine, causing a race condition.
解决方案
-
90% 成功率
Ensure each query uses its own connection properly: use db.QueryContext with a context and avoid sharing rows between goroutines.
-
75% 成功率
Use a mutex to serialize access to rows: var mu sync.Mutex; mu.Lock(); defer mu.Unlock(); rows.Scan(...)
无效尝试
常见但无效的做法:
-
80% 失败
May cause data corruption or panics due to concurrent access.
-
50% 失败
Doesn't fix the root cause; may delay the issue but not prevent it.