// libSQL error reference

SQLITE_BUSY: database is locked: embedded replica file locked

A local embedded replica file could not take or release its lock, usually because another process has the same replica file open for writing.

エラー
SQLITE_BUSY: database is locked
名前
5
カテゴリ
Replicas and consistency
エラーメッセージ
SQLITE_BUSY: database is locked

意味

An embedded replica is a local SQLite file that syncs from the remote database. Local reads are plain SQLite, and the sync loop writes the replicated frames into the same file, so local locking rules apply. The file allows one writer at a time. Two processes opening the same replica file, or a long local write while the replication loop applies frames, collide on the file lock and SQLite returns SQLITE_BUSY with the text database is locked. Turso's docs describe the layout: reads run locally from the file, and writes are sent to the cloud primary and then reflected back to the replica, so the local file is being written both by your process and by the replication loop.

よくある原因

  • Two processes or app instances opened the same replica file, and both tried to write.
  • A local write held the file lock while sync() tried to apply replicated frames.
  • A crashed process left the file's lock bookkeeping in a state a new opener cannot take.
  • A raw BEGIN IMMEDIATE in the replication-enabled libSQL build can return busy immediately instead of waiting on busy_timeout.

Find the cause

List every process that opens the replica file: `lsof replica.db` shows holders. More than one writer is the cause. If only one process holds it, check whether the error arrives during `sync()` after a long local write, which points at lock contention between your transaction and the replication loop. After a crash, open the file read-only (`sqlite3 'file:replica.db?mode=ro' 'PRAGMA integrity_check;'`) to test whether the file itself is readable.

修正方法

  1. Give each process its own replica file, or route writes through one process:

    // process A
    const dbA = createClient({ url: 'file:replica-a.db', authToken: token });
    // process B
    const dbB = createClient({ url: 'file:replica-b.db', authToken: token });
  2. Set a busy timeout for the local file so SQLite waits instead of failing fast. In @libsql/client the timeout option covers file: URLs:

    const db = createClient({ url: 'file:replica.db', syncUrl, authToken, timeout: 5000 });
  3. Keep local write transactions short and sync outside them:

    await db.sync();   // outside any open transaction
  4. If a crashed process left the file wedged, check for stale locks by opening it read-only:

    sqlite3 replica.db 'PRAGMA integrity_check;'

Other ways to handle it

使用 libsql:// directly for all writes and keep the replica read-only. That removes the local write lock entirely, at the cost of network round trips on writes.

安全に再現する

Local and honest: create two clients on the same file URL and make both write in a loop; the second writer hits `LibsqlError: SQLITE_BUSY: database is locked`. Or with plain sqlite3: hold a transaction open in one shell and run `BEGIN IMMEDIATE` in another on the same file. No remote account needed; the cloud primary never sees the contention.

質問

Do embedded replicas support writes?

Yes. Per Turso's docs, writes on an embedded replica are sent to the cloud primary and then reflected back to the local file; reads always run locally. That round trip still touches the local file, so local file locks matter.

Can two processes share one embedded replica file?

Reads can coexist, but two writers on the same local file collide with SQLITE_BUSY. Give each process its own replica file, or route writes through the remote libsql:// URL and keep the replicas read-only.

How do I set a busy timeout for a file: database in @libsql/client?

合格 timeout in milliseconds to createClient({ url: 'file:...', timeout: 5000 }). The client applies it to every connection it opens, and it only takes effect for local SQLite databases; remote clients ignore it, and Turso Cloud does not support PRAGMA busy_timeout まったく。

関連エラー

More replicas and consistency errors

コードとメッセージのテキストは、 libSQL / Turso documentation。全容は次で参照 libSQL error list または すべてのデータベースエラーを検索.

// エラーが減り、修正も速く

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.

このページのテキストとデータ: CC BY 4.0、出典は SQLly (sqlly.app) と明記してください。 コードと SQL スニペット: MIT. ライセンスの詳細 引用したエラーメッセージの文言は、各データベースベンダーに帰属します。