debugMajor
SQLite database is locked error under concurrent access
Viewed 0 times
SQLITE_BUSYdatabase lockedWALjournal_modebusy_timeoutconcurrent writes
nodejsdockerlinuxmacos
Error Messages
Problem
SQLite throws SQLITE_BUSY or database is locked when multiple processes or threads try to write simultaneously. Common in web servers handling concurrent requests.
Solution
(1) Enable WAL mode: PRAGMA journal_mode=WAL — allows concurrent readers with one writer. (2) Set busy timeout: PRAGMA busy_timeout=5000 — waits up to 5s instead of failing immediately. (3) Keep write transactions short — do reads outside transactions. (4) Use a single connection pool with serialized writes for web servers. (5) If using better-sqlite3 in Node, it is synchronous and handles this naturally. For true concurrent writes, consider PostgreSQL instead.
Why
SQLite uses file-level locking. In default journal mode, any write locks the entire database. WAL mode allows concurrent reads during writes but still serializes writers.
Revisions (0)
No revisions yet.