go runtime_error ai_generated true

sql:事务已提交或回滚

sql: transaction has already been committed or rolled back

ID: go/database-sql-tx-committed

其他格式: JSON · Markdown 中文 · English
80%修复率
88%置信度
0证据数
2024-07-12首次发现

版本兼容性

版本状态引入弃用备注
1.21 active

根因分析

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

English

Attempting to execute a query on a transaction that has already been committed or rolled back.

generic

解决方案

  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.

无效尝试

常见但无效的做法:

  1. 90% 失败

    Once a transaction is done, it cannot be reused.

  2. 80% 失败

    Commit() can only be called once.