# sql: 连接已被使用

- **ID:** `go/database-sql-conn-already-in-use`
- **领域:** go
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

数据库连接在仍被其他 goroutine 使用时被返回到池中，导致竞争条件。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 1.16 | active | — | — |
| 1.19 | active | — | — |

## 解决方案

1. **** (90% 成功率)
   ```
   Ensure each query uses its own connection properly: use db.QueryContext with a context and avoid sharing rows between goroutines.
   ```
2. **** (75% 成功率)
   ```
   Use a mutex to serialize access to rows: var mu sync.Mutex; mu.Lock(); defer mu.Unlock(); rows.Scan(...)
   ```

## 无效尝试

- **** — May cause data corruption or panics due to concurrent access. (80% 失败率)
- **** — Doesn't fix the root cause; may delay the issue but not prevent it. (50% 失败率)
