# sqlite3.OperationalError: cannot rollback - no transaction is active

- **ID:** `database/sqlite-cannot-rollback-no-savepoint`
- **Domain:** database
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 95%

## Root Cause

A ROLLBACK command was issued without a corresponding BEGIN TRANSACTION, or a savepoint name was used that does not exist.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| SQLite 3.42.0 | active | — | — |
| SQLite 3.43.2 | active | — | — |
| SQLite 3.44.0 | active | — | — |

## Workarounds

1. **Wrap the ROLLBACK in a try-except block and only execute it if a transaction is active. In Python: `try: cursor.execute('ROLLBACK') except sqlite3.OperationalError: pass`. Alternatively, use a context manager: `with connection: cursor.execute(...)` which auto-commits/rolls back.** (95% success)
   ```
   Wrap the ROLLBACK in a try-except block and only execute it if a transaction is active. In Python: `try: cursor.execute('ROLLBACK') except sqlite3.OperationalError: pass`. Alternatively, use a context manager: `with connection: cursor.execute(...)` which auto-commits/rolls back.
   ```
2. **Review the code to ensure every ROLLBACK is paired with a BEGIN TRANSACTION. Use a connection wrapper that tracks transaction state (e.g., a flag `in_transaction` set to True on BEGIN and False on COMMIT/ROLLBACK).** (90% success)
   ```
   Review the code to ensure every ROLLBACK is paired with a BEGIN TRANSACTION. Use a connection wrapper that tracks transaction state (e.g., a flag `in_transaction` set to True on BEGIN and False on COMMIT/ROLLBACK).
   ```

## Dead Ends

- **** — If the code already has a transaction, this creates nested transactions that may not be handled correctly, leading to 'cannot commit - no transaction is active' errors later. (60% fail)
- **** — The WAL mode affects concurrency and crash recovery, not transaction management. It does not fix missing transactions. (90% fail)
