
SQLite's ROLLBACK TO sounds like it ends an unsuccessful transaction. It does not. It rewinds changes to a named savepoint while leaving the transaction open. Confusing that with a plain ROLLBACK can leave a database tool holding unfinished work, or lead an application to commit something its author thought had been canceled.
I would learn the distinction with disposable data before adding savepoints to an import or maintenance script. The useful mental model is a bookmark inside a transaction, not a second independent transaction.
Try a small example away from real data
Use a fresh temporary database with no existing transaction. The table below is only demonstration data. Run the statements through the same connection:
CREATE TABLE tasks (
id INTEGER PRIMARY KEY,
state TEXT NOT NULL
);
INSERT INTO tasks VALUES (1, 'new'), (2, 'new');
BEGIN;
UPDATE tasks SET state = 'reviewed' WHERE id = 1;
SAVEPOINT optional_edit;
UPDATE tasks SET state = 'archived' WHERE id = 2;
SELECT * FROM tasks ORDER BY id;
The query shows task 1 as reviewed and task 2 as archived. Both edits are still inside the outer transaction.
Now undo only the later part:
ROLLBACK TO optional_edit;
SELECT * FROM tasks ORDER BY id;
Task 1 remains reviewed; task 2 is back to new. The earlier update happened before the savepoint, so this rollback did not cancel it.
The bookmark and the transaction still exist
SQLite documents that ROLLBACK TO leaves the named savepoint on the transaction stack. It removes later savepoints, if any, but it does not finish the outer transaction.
After a rollback to the bookmark, release that bookmark when you no longer need it:
RELEASE optional_edit;
In this example, releasing the inner savepoint still does not commit task 1's update. The surrounding BEGIN transaction remains active. You must deliberately finish that transaction according to the operation's intended outcome.
For this disposable demonstration, cancel the remaining work:
ROLLBACK;
SELECT * FROM tasks ORDER BY id;
Both tasks are now new. I ran this sequence against an in-memory SQLite database: the row values followed these steps, and the connection remained in a transaction after both ROLLBACK TO and the inner RELEASE. That is a limited local check, not a test of your database wrapper or production workflow.
Why RELEASE is not always a commit
Context matters. An inner savepoint's release removes a rollback point; its changes can still be undone by the outer transaction. However, if a savepoint is the outermost transaction, releasing it empties the transaction stack and commits.
Do not copy a RELEASE statement between those situations without checking who owns the surrounding transaction. A framework or database browser may already manage that boundary. SQLite also rejects a new BEGIN inside an existing transaction; savepoints provide the named nesting mechanism instead.
Use the distinction as an error-handling rule
Before using a savepoint in application code, decide what failure means. Should the application undo one optional operation and continue, or cancel the entire transaction? A partial rewind and a full cancellation are different outcomes.
Check every command's result. A missing savepoint name produces an error, and some database errors can change transaction state. Avoid blindly continuing to a commit after a failed rollback. Also, this example assumes normal rollback support; SQLite warns that rollback behavior is undefined when journal_mode is OFF.
A savepoint is useful when its scope is understood. It is not a backup, permission to experiment on live records, or proof that an operation is complete.
Comments
Post a Comment