SQLite documents its transactions as SERIALIZABLE; how does it provide that between connections, and where does that isolation stop applying?
answer
- writes are actually serialized
- readers see committed data only
- a stale snapshot cannot write
- same connection sees its own changes
- shared cache plus read_uncommitted
basics
~20 sSQLite gets serializable isolation by actually serializing writes: one writer at a time, and readers see only committed data. The exceptions are statements on the same connection, which see each other's uncommitted changes, and shared-cache connections with PRAGMA read_uncommitted on.
solid answer
~40 sBetween two connections, in the same thread, other threads or other processes, a reader sees only complete committed transactions. SQLite does not need predicate locks or commit-time conflict checks, because only one write transaction runs at a time. In rollback-journal mode readers are locked out while the writer writes the file; in WAL mode each read transaction keeps a snapshot, and a read transaction whose snapshot has gone stale cannot become a writer (`SQLITE_BUSY_SNAPSHOT`), so history never forks. There is no `SET TRANSACTION ISOLATION LEVEL`; the only knob is `PRAGMA read_uncommitted`, which matters only between connections sharing a cache, a mode the documentation calls obsolete. Within one connection there is no isolation: a `SELECT` sees that connection's uncommitted changes, and changing a table while stepping a `SELECT` over it gives undefined results.
go deeper
Remember that SQLite transactions are serializable between connections, and that a connection sees its own uncommitted changes.
Explain that serializability comes from one writer at a time, with locks in rollback mode and snapshots plus SQLITE_BUSY_SNAPSHOT in WAL mode.
Show the traps you would catch in review: code relying on mid-loop visibility on one connection, and anyone reaching for read_uncommitted or shared cache.
Explain to a team arriving from a server database why SQLite has no isolation knob, and steer contention fixes towards WAL mode and transaction shape instead.
## Isolation between connections A **database connection** is one `sqlite3` object returned by `sqlite3_open()`. When two connections use the same database file, SQLite guarantees that the reader sees only **complete, committed transactions** from the writer; nothing a writer has done before its `COMMIT` is visible to anyone else. That holds whether the two connections are in one thread, in different threads of one process, or in different processes. The documentation's summary is short: transactions in SQLite are **SERIALIZABLE**. ## How SQLite gets there: by actually serializing the writes Server engines often reach serializability with predicate locks or with conflict detection that aborts transactions at commit. SQLite takes a blunter route: - **Only one write transaction at a time.** Every connection may write, but they take turns, and SQLite's locks enforce this automatically. With a single writer, writes are already in a serial order. - **Rollback-journal mode:** readers are locked out while a writer puts its changes into the database file, so they never see a half-written transaction. - **WAL mode:** each read transaction reads a fixed snapshot of the database taken when it began, and a transaction that commits meanwhile stays invisible to it. The documentation calls this snapshot isolation. - **A stale snapshot cannot write.** If a read transaction tries to write after another connection has committed, it fails with `SQLITE_BUSY_SNAPSHOT` instead of writing on an out-of-date view. Two transactions that both read and then write therefore cannot both commit on overlapping views. ## No isolation level to choose | Situation | What a reader sees | |---|---| | Another connection, ordinary use | Only committed transactions | | Another connection in WAL mode, read transaction open | The snapshot from when its read transaction began | | Same connection, earlier statement in its transaction | Its own uncommitted changes | | Same connection, change made while a `SELECT` is still stepping | Undefined: the change may or may not appear | | Shared-cache connections with `PRAGMA read_uncommitted` on | Another connection's uncommitted changes | SQLite's grammar has no `SET TRANSACTION ISOLATION LEVEL` statement, and `BEGIN` accepts only `DEFERRED`, `IMMEDIATE` or `EXCLUSIVE`, which choose when locks are taken, not what is visible. The one visibility setting is `PRAGMA read_uncommitted`, off by default, and it matters only between connections that share a page and schema cache. Shared-cache mode is disabled by default, and the documentation calls it obsolete and discourages its use. Outside it, turning `read_uncommitted` on changes nothing: serializable isolation is still used. ## No isolation inside one connection Isolation is between connections, not between the statements of one connection. Within connection X: 1. After X runs `BEGIN IMMEDIATE` and some `INSERT`s, a `SELECT` on X sees those rows before they are committed; a `SELECT` on another connection does not. 2. A `SELECT` on X sees every change completed on X before the `SELECT` started, committed or not. 3. If X changes a table while it is still stepping through a `SELECT` over that table, whether the `SELECT` sees the change is **undefined**. It can depend on the SQLite release, the schema, whether `ANALYZE` has run and the query itself; a changed row may come back twice, or a deleted row may still appear. These operations are safe for the database file, only unpredictable in their results. Deleting the current row, or one already returned, by `rowid` while looping is safe; relying on the `SELECT` to skip or include rows changed ahead of it is not. ## What it means in practice - **Need isolation? Use another connection.** Work that must not see in-progress changes, such as a report running beside a batch import, needs its own connection. - **A weaker level is not on the menu.** Contention comes from the single writer and is reduced by WAL mode and short write transactions, not by an isolation setting. - **Expect SQLITE_BUSY where other engines would block on a row or abort at commit.** The price of serialized writes is that the second writer waits or is refused. - **Collect, then modify.** When a loop must update the rows it reads, read the keys first, finish the `SELECT`, then write.
- In SQLite, is it safe to delete rows from a table while stepping through a SELECT over that table on the same connection?It is safe for the database: deleting the current row or any row already returned, by `rowid`, works. Deleting a row the query has not reached yet is also harmless, but that row may still turn up later; rows inserted or updated during the loop may or may not appear. The results are undefined, so collect keys first if correctness depends on them.
- Can a SQLite application choose READ COMMITTED to reduce locking?No. SQLite has no `SET TRANSACTION ISOLATION LEVEL`, and `BEGIN DEFERRED`, `IMMEDIATE` and `EXCLUSIVE` only choose when locks are taken. `PRAGMA read_uncommitted` relaxes isolation only between connections sharing a cache, a mode the documentation discourages. To reduce contention, use WAL mode so readers never wait for the writer, and keep write transactions short.
- SQLite's default isolation is READ COMMITTED, as in many server databases.
- Statements on one SQLite connection are isolated from each other's uncommitted writes.
- PRAGMA read_uncommitted lets any SQLite connection read another's uncommitted rows.
- SQLite reaches serializability by checking conflicts at COMMIT and aborting one side.
- SET TRANSACTION ISOLATION LEVEL picks a level per transaction in SQLite.