# sql: transaction has already been committed or rolled back

- **ID:** `go/database-sql-tx-commit-rollback`
- **Domain:** go
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

Calling Commit() or Rollback() on a transaction that has already been finalized, leading to an error.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 1.14 | active | — | — |
| 1.18 | active | — | — |

## Workarounds

1. **** (90% success)
   ```
   Use defer with a check: defer func() { if err := tx.Rollback(); err != sql.ErrTxDone { log.Fatal(err) } }()
   ```
2. **** (85% success)
   ```
   Track transaction state with a boolean: committed := false; if err := tx.Commit(); err == nil { committed = true }
   ```

## Dead Ends

- **** — Transaction state is already final; continuing may lead to data inconsistency. (70% fail)
- **** — Rollback will also fail because the transaction is already done. (90% fail)
