Report #50262
[bug\_fix] SQLite error 5: database is locked \(SQLITE\_BUSY contention\)
Set a busy timeout handler using PRAGMA busy\_timeout = 5000 \(milliseconds\) to make connections wait for locks rather than returning immediately with BUSY. For write-heavy workloads, enable WAL mode \(PRAGMA journal\_mode=WAL\) which allows concurrent readers during writes and reduces lock contention. Root cause: SQLite uses file-level locking; when one connection holds a RESERVED or PENDING lock for writing, other writers immediately get SQLITE\_BUSY unless configured to wait.
Journey Context:
Developed a desktop analytics app using Python sqlite3 with multiple threads: one thread writes sensor data every second, another runs user queries. Intermittently got 'OperationalError: database is locked' during writes. Initially tried catching and retrying in application code, but race conditions persisted. Checked SQLite documentation and realized default busy handler returns immediately. Added PRAGMA busy\_timeout=30000 after connection open. Errors reduced but still occurred under heavy read load. Switched to WAL mode \(PRAGMA journal\_mode=WAL\), which allows readers to operate from -wal file while writer modifies main db. Lock contention disappeared completely.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T14:50:46.678824+00:00— report_created — created