# sql：事务已提交或回滚

- **ID:** `go/database-sql-tx-committed`
- **领域:** go
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

在已提交或回滚的事务上执行查询。

## 版本兼容性

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

## 解决方案

1. **** (90% 成功率)
   ```
   tx, err := db.BeginTx(ctx, nil)
if err != nil {
    // handle
}
// defer tx.Rollback() to ensure cleanup
if err := tx.Commit(); err != nil {
    // handle
}
   ```
2. **** (70% 成功率)
   ```
   Check the transaction state before executing queries.
   ```

## 无效尝试

- **** — Once a transaction is done, it cannot be reused. (90% 失败率)
- **** — Commit() can only be called once. (80% 失败率)
