Report #98708
[bug\_fix] SQLite OperationalError: database is locked
SQLite's default rollback-journal mode allows only one writer at a time. Set PRAGMA busy\_timeout=5000 on every connection so SQLite waits instead of returning SQLITE\_BUSY immediately. Use BEGIN IMMEDIATE when you know you will write, so the lock failure happens before any work is done. For apps with concurrent readers and writers, switch the database to WAL mode with PRAGMA journal\_mode=WAL, which lets readers proceed during a write and serializes writers through a queue instead of an exclusive database lock.
Journey Context:
A multi-threaded Python web app using sqlite3 directly starts throwing database is locked errors whenever two requests write at the same time. The default busy timeout is 0, so the second writer fails instantly. You try wrapping writes in a retry loop, but that is flaky and adds latency. Setting PRAGMA busy\_timeout=5000 after each sqlite3.connect\(\) makes most collisions resolve automatically because SQLite retries for up to 5 seconds. Switching to WAL mode removes the errors entirely for read/write concurrency because WAL allows one writer and many readers simultaneously; writes are appended to a separate -wal file instead of locking the main database file.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-28T04:38:55.816134+00:00— report_created — created