COMMIT
COMMIT ends the current transaction and persists all pending changes.
Until commit, transaction writes are not finalized.
Core Concepts
flowchart LR
A[BEGIN transaction] --> B[Pending writes]
B --> C[COMMIT]
C --> D[Changes become durable]
| State | Meaning |
|---|---|
Before COMMIT | Changes are part of open transaction |
After COMMIT | Changes are finalized |
Code Examples
-- Setup table for commit demonstration.
CREATE TABLE tx_commit_demo (
id INTEGER PRIMARY KEY,
value TEXT NOT NULL
);
| Expected output |
|---|
| Table created. |
-- Open transaction, write data, and commit.
BEGIN;
INSERT INTO tx_commit_demo (value)
VALUES ('alpha'), ('beta');
COMMIT;
| Expected output |
|---|
| Two rows committed successfully. |
-- Read committed data.
SELECT id, value
FROM tx_commit_demo
ORDER BY id;
| id | value |
|---|---|
| 1 | alpha |
| 2 | beta |
SQLite-Specific Nuances
SQLite Nuance
In SQLite, END TRANSACTION is accepted as a synonym for COMMIT.
Common Pitfalls / Best Practices
Pitfall
Assuming writes are permanent immediately after each statement when inside an explicit transaction.
Best Practice
Treat COMMIT as the single success checkpoint for the whole transaction block.
Quick Challenge
Insert gamma into tx_commit_demo within a transaction and finalize it.
View Solution
BEGIN;
INSERT INTO tx_commit_demo (value)
VALUES ('gamma');
COMMIT;
| Expected output |
|---|
| Row is persisted after commit. |