Isolation and concurrency
How SQLite connections share data and coordinate reads and writes.
A transaction sees its own writes before committing. Separate connections normally see only committed changes. See SQLite's isolation guide.
Readers and writers
SQLite supports multiple readers, but only one writer at a time for a database file. Competing writes can fail with SQLITE_BUSY. A bounded busy_timeout can wait for temporary lock contention; it does not guarantee success.
Journal modes
Rollback journaling preserves original pages in a journal while changes are written to the database. Readers and writers can block each other.
Write-ahead logging, or WAL, writes changes to a separate log so readers can continue while a writer works:
PRAGMA journal_mode = WAL;A WAL read transaction sees a snapshot of committed data from when it began. It must end before a new transaction can see later commits. Long-lived readers can delay checkpoints and let the WAL file grow. See SQLite's WAL documentation.
Connection settings
WAL mode persists in the database file. Settings such as busy_timeout and foreign_keys belong to each connection and need to be set again when opening another one.
For Nitro SQLite APIs, see multiple connections. For the order of calls on one connection, see sync and async.
Cursor locks and errors
A cursor keeps a read transaction open between pages. In rollback-journal mode,
that read lock prevents a writer from committing. With the default zero busy
timeout, an autocommit write or explicit COMMIT fails immediately with
SQLITE_BUSY. Close or exhaust the cursor before retrying the write. A writer's
busy timeout only waits for a bounded time. It cannot release an idle cursor's
lock. A synchronous write can also prevent JavaScript from closing that cursor
until the timeout expires.
WAL allows a writer to commit while a cursor holds its snapshot. The snapshot can still prevent a checkpoint from truncating the WAL. Choose the journal mode for your application's concurrency needs and close partial reads promptly.
Cursor preparation and page errors expose SQLite's result code as
SQLiteError.sqliteCode. Use error.sqliteCode & 0xff to read the primary
code. Code 5 is SQLITE_BUSY and code 6 is SQLITE_LOCKED. Not all native
operations report a code, so check for undefined. A failed page closes its
cursor. After releasing the conflicting lock, open a new cursor to retry the
query. Do not retry a page on the failed cursor.