SQLITE_BUSY: SQLITE_BUSY: the database is busy
A write could not start or commit because another connection held the lock: on remote writes this is one writer contending with itself across connections.
- Error
SQLITE_BUSY- Name
5- Category
- Local SQLite errors
What it means
SQLITE_BUSY (5) means SQLite could not get the lock it needed because another connection was using the database. On a local file that is classic multi-process contention. On a remote libSQL server the same code surfaces when writes contend at the server, because sqld is still SQLite underneath with one writer at a time. The libsql JS client renders it as SQLITE_BUSY: database is locked.
Common causes
- Two connections wrote to the same remote database at once and one lost the race.
- A local file was opened by more than one process.
- No busy_timeout was set, so SQLite gave up instead of waiting.
- A long transaction held the write lock past other clients' patience.
Find the cause
Look at the timing: a busy error under concurrent writes that goes away with retries is ordinary contention, and busy_timeout plus backoff is the fix. One that never clears points at a stuck transaction: check for long-open `BEGIN` blocks in your code, and on a local file check `lsof` for other processes. On remote databases, correlate the error timestamps with your request concurrency.
How to fix it
-
On a local file, set a busy timeout so SQLite waits out short contention. On a remote Turso database the pragma is not supported, so retries with backoff are the mechanism there:
PRAGMA busy_timeout = 5000; // @libsql/client local file: createClient({ url: 'file:local.db', timeout: 5000 }) -
Retry failed writes with backoff instead of hammering:
for (let i = 0; i < 5; i++) { try { await db.execute('UPDATE t SET v = v + 1 WHERE id = 1'); break; } catch (e) { if (!String(e).includes('SQLITE_BUSY')) throw e; await new Promise(r => setTimeout(r, 100 * 2 ** i)); } } -
Batch writes so a burst commits as one operation instead of many lock races:
await db.batch([ 'UPDATE a SET x = 1', 'UPDATE b SET y = 2' ]);
-
On a local file, find who else holds it open:
lsof mydata.db
Other ways to handle it
Serialize writes through a single connection or queue. For remote databases, batches reduce the number of write transactions and so the number of races. PRAGMA journal_mode=WAL helps local file concurrency for readers but does not remove the single-writer rule, and Turso Cloud does not allow changing the journal mode at all.
Reproduce it safely
Local, honest: in one sqlite3 shell run `BEGIN; INSERT INTO t VALUES (1);` and leave it open. In another shell run `INSERT INTO t VALUES (2);` and it returns `database is locked` (SQLITE_BUSY). Needs any scratch file: `sqlite3 scratch.db 'CREATE TABLE t (a);'`. The remote flavor reproduces with two @libsql/client connections writing concurrently to the same test database.
Questions
Does PRAGMA busy_timeout work on Turso Cloud?
No. Turso's limitations page lists busy_timeout as not supported: the cloud manages concurrency internally and the pragma has no effect. For remote writes, retry with backoff instead; for local file: databases the pragma works, and @libsql/client's timeout option sets the same thing.
Why do I get SQLITE_BUSY on an embedded replica but not on the remote database?
The replica is a local SQLite file with one writer at a time, so local lock rules apply on top of the remote behavior. See the embedded-replica-locked entry: two processes or a sync during a local write are the usual causes.
What is the difference between SQLITE_BUSY and SQLITE_LOCKED?
BUSY means the commit-level write lock was held by another connection; LOCKED means a needed table-level lock (often a shared-cache conflict) was unavailable. On remote libSQL servers the code that surfaces is SQLITE_BUSY.
Related errors
SQLITE_BUSY: database is lockedembedded replica file locked429 Too Many Requestsrequest rate limit hittoo many connectionsToo many connections to one database
More local sqlite errors errors
The code and message text are from the libSQL / Turso documentation. Browse the full libSQL error list or search every database error.
Work on libSQL in SQLly
A native SQL client for macOS, Windows and Linux with schema-aware IntelliSense, guardrails for risky statements and clear error messages, for libSQL and the other databases you use.
Text and data on this page: CC BY 4.0, credit SQLly (sqlly.app). Code and SQL snippets: MIT. License details Quoted error message text belongs to its database vendor. View as Markdown