// libSQL error reference

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
Nombre
5
Categoría
Local SQLite errors
Mensaje de error
SQLITE_BUSY

Qué significa

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.

Causas comunes

  • 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.

Cómo solucionarlo

  1. 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 })
  2. 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));
      }
    }
  3. 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'
    ]);
  4. 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.

Reprodúcelo de forma segura

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.

Preguntas

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.

¿Cuál es la diferencia entre SQLITE_BUSY y 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.

Errores relacionados

More local sqlite errors errors

El código y el texto del mensaje son de libSQL / Turso documentation. Explora la lista completa de libSQL error list o buscar cada error de base de datos.

// menos errores, correcciones más rápidas

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.

Texto y datos de esta página: CC BY 4.0, con atribución a SQLly (sqlly.app). Código y fragmentos SQL: MIT. Detalles de la licencia El texto citado de los mensajes de error pertenece a su proveedor de base de datos.