SQLite Transactions, WAL, and Locking Cheatsheet

BEGIN modes, COMMIT and ROLLBACK, savepoints, WAL mode, busy timeouts, and concurrency patterns for SQLite applications.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all

Transactions and savepoints

Control atomic writes and partial rollback behavior.

Start a transaction

Bundle multiple statements atomically.

sqlANYsqlitetransactioncommit
sql
BEGIN;
INSERT INTO audit_log(action) VALUES ('start');
UPDATE counters SET value = value + 1 WHERE key = 'jobs';
COMMIT;
Notes

Wrapping related writes in one transaction improves consistency and often performance.

Begin an IMMEDIATE transaction

Reserve the write lock up front.

sqlANYsqlitebegin immediatelocking
sql
BEGIN IMMEDIATE;
Notes

Useful when you know the transaction will write and want to fail early if the database is busy.

Roll back a transaction

Undo uncommitted changes.

sqlANYsqliterollback
sql
ROLLBACK;
Notes

A safety valve when a migration, import, or manual edit goes wrong.

Use a savepoint

Create a nested rollback point within a transaction.

sqlANYsqlitesavepoint
sql
SAVEPOINT import_batch;
INSERT INTO tags(name) VALUES ('sqlite');
ROLLBACK TO import_batch;
RELEASE import_batch;
Notes

Helpful for large imports or multi-step workflows where one chunk may fail.

WAL mode and lock handling

Improve write concurrency and handle busy databases gracefully.

Enable WAL journaling

Switch the database to write-ahead logging mode.

sqlANYsqlitewaljournal_mode
sql
PRAGMA journal_mode = WAL;
Notes

WAL often improves read/write concurrency for app-style workloads.

Set a busy timeout

Wait briefly when the database is locked.

sqlANYsqlitebusy_timeoutlocks
sql
PRAGMA busy_timeout = 5000;
Notes

A practical first-line defense against transient `database is locked` failures.

Run a WAL checkpoint

Merge WAL contents back into the main database file.

sqlANYsqlitecheckpointwal
sql
PRAGMA wal_checkpoint(TRUNCATE);
Notes

Useful in maintenance jobs or after heavy write bursts when you want to reclaim WAL size.

Inspect locking mode

See or set the database locking mode.

sqlANYsqlitelocking_modepragma
sql
PRAGMA locking_mode;
Notes

Useful when debugging exclusive vs normal locking behavior.

Recommended next

No recommendations yet.