# error: sql: transaction has already been committed or rolled back

- **ID:** `go/database-sql-transaction-commit-before-rollback`
- **Domain:** go
- **Category:** data_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

Attempting to commit or rollback a transaction that has already been finalized, often due to double call or logic error.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 1.20 | active | — | — |
| 1.21 | active | — | — |

## Workarounds

1. **Use a helper function to manage transaction lifecycle** (95% success)
   ```
   func withTx(db *sql.DB, fn func(*sql.Tx) error) error {
    tx, err := db.Begin()
    if err != nil { return err }
    defer tx.Rollback()
    if err := fn(tx); err != nil { return err }
    return tx.Commit()
}
   ```

## Dead Ends

- **Calling Rollback after Commit in defer** — Defer executes after Commit, causing panic; use flag to check. (80% fail)
