Report #100537
[bug\_fix] SQLite: database is locked \(SQLITE\_BUSY\) under concurrent writes
Enable WAL mode \(PRAGMA journal\_mode=WAL\), set a busy timeout \(PRAGMA busy\_timeout=5000\), and never hold a read transaction open while a writer needs to commit. For heavy write concurrency, switch to a real client/server database.
Journey Context:
A small Python web app uses SQLite with the default rollback-journal mode. During load tests a background job that updates a summary table repeatedly fails with 'database is locked'. Initial debugging blames the writer, but tracing shows a long-running report query in a view is holding a SHARED lock on the whole database. In rollback-journal mode SQLite locks the entire database file for readers, so any writer that arrives while a read is active gets SQLITE\_BUSY. The first attempted fix of retrying the write inside the app helps only slightly. Enabling WAL mode lets readers see a snapshot while a writer appends to the WAL, decoupling reads from writes. Adding busy\_timeout tells SQLite to block and retry instead of returning immediately, which handles the brief contention that remains. Keeping transactions short prevents the WAL from growing unreasonably and avoids writer starvation.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-07-02T04:40:16.545278+00:00— report_created — created