skip to content

In SQLite, what does SQLITE_BUSY mean, and how does setting busy_timeout change what a connection does when it meets one?

level: middleimportance: must knowfreq 72%

answer

  1. another connection holds the lock
  2. default handler: none at all
  3. sleep and retry, in milliseconds
  4. one handler per connection

basics

~20 s

SQLITE_BUSY, reported as 'database is locked', means another connection holds a lock this one needs. With no busy handler, the default, it returns at once; PRAGMA busy_timeout installs a handler that sleeps and retries for up to that many milliseconds.

solid answer

~40 s

`SQLITE_BUSY` (code 5) is a conflict with a different connection, in another thread or process; `SQLITE_LOCKED` (code 6) is a conflict inside the same connection or with one sharing its cache. `SQLITE_BUSY` can arrive at `BEGIN`, at any write, or at `COMMIT`. By default a connection has no busy handler, so it gets the error the moment it meets the lock. `PRAGMA busy_timeout = 5000`, or `sqlite3_busy_timeout()`, installs a handler that sleeps and retries until at least 5000 ms of sleeping has accumulated, then gives up with `SQLITE_BUSY`; zero or less turns it off. A connection has one busy handler, so setting a timeout replaces any custom one, and it is per connection, so every connection needs it. SQLite also skips the handler when waiting could deadlock.

code

sql · 2 lines
sql
PRAGMA busy_timeout = 5000;
PRAGMA busy_timeout;

go deeper

for a junior

Recall that SQLITE_BUSY, 'database is locked', means another connection holds the lock, and that by default SQLite does not wait.

for a middle

Explain how busy_timeout works in milliseconds, that a connection has one handler, and how SQLITE_BUSY differs from SQLITE_LOCKED.

for a senior

Show you can tell brief contention a timeout absorbs from the cases it cannot: long write transactions, read-to-write upgrades, and exclusive locking mode.

for a principal

Set the team default: a timeout on every connection, short write transactions, and treating persistent SQLITE_BUSY as a sign the one-writer design has hit its ceiling.

## What the error says `SQLITE_BUSY` (result code 5, message `database is locked`) means the database file could not be written, or in some cases read, because of **concurrent activity on another database connection**: another thread with its own connection, or another process. It is the normal signal that SQLite's one-writer-at-a-time rule has been hit, not a sign of damage. | Code | Number | Meaning | |---|---|---| | `SQLITE_BUSY` | 5 | Conflict with a different connection | | `SQLITE_LOCKED` | 6 | Conflict inside the same connection, or with a connection sharing its cache | | `SQLITE_BUSY_RECOVERY` | 261 | Another process is recovering a WAL-mode database after a crash | | `SQLITE_BUSY_SNAPSHOT` | 517 | WAL mode: a read transaction cannot become a write because its snapshot is stale | For historical compatibility the C interfaces return primary codes by default, so the extended codes arrive as plain `SQLITE_BUSY` unless the application reads `sqlite3_extended_errcode()` or turns extended result codes on. ## Where it shows up `SQLITE_BUSY` can arrive at any point in a transaction: - at `BEGIN IMMEDIATE` or `BEGIN EXCLUSIVE`, when another connection is already writing; - at the first write of a deferred transaction, for the same reason; - at `COMMIT` in rollback-journal mode, when readers still hold locks the commit must wait out; the transaction stays open and the `COMMIT` can be retried; - on a read, in rollback-journal mode while a writer is committing, and in WAL mode in a few rarer cases such as another connection running crash recovery. ## The busy handler and busy_timeout What a connection does when it meets a lock is decided by its **busy handler**: 1. **The default is no handler.** With none set, SQLite returns `SQLITE_BUSY` the moment it meets the lock. 2. **`sqlite3_busy_timeout(db, ms)` or `PRAGMA busy_timeout = ms`** installs a handler that sleeps, retries and sleeps again until at least `ms` milliseconds of sleeping have accumulated; then it gives up and the call returns `SQLITE_BUSY`. The pragma exists for language bindings that do not expose the C function, and `PRAGMA busy_timeout;` reads the current value. 3. **A value of zero or less turns busy handling off.** 4. **One handler per connection.** Setting a timeout clears any custom handler registered with `sqlite3_busy_handler()`, and registering a custom handler replaces the timeout. 5. **It belongs to the connection.** The busy handler is set on each database connection, so every connection the application opens needs it, including each one a pool creates. 6. **It is not always called.** If SQLite determines that invoking the handler could deadlock, it returns `SQLITE_BUSY` without calling it. ## The request-log service A service whose worker threads each hold a connection and insert request rows in autocommit mode is the textbook case. Each `INSERT` is its own write transaction, so when two threads insert at the same moment one of them meets the other's write lock. With no busy handler, that thread gets `database is locked` at once, even though the other insert would have finished within milliseconds. The usual repair has three parts: - set a busy timeout, a few seconds is common, on **every** connection as it is opened; - group inserts into short explicit transactions, so the service takes the write lock fewer times and holds it briefly; - switch the database to WAL mode, so threads and dashboards that only read stop competing with the writer. After the change, look at when any remaining `database is locked` arrives. After roughly the timeout means some writer holds its transaction too long; instantly, with the timeout set, points at a transaction that read before it wrote. ## What a timeout cannot fix A busy timeout turns brief contention into a short wait. It does not raise the ceiling of one writer at a time, so a writer that holds its transaction open longer than the timeout still makes the others fail. It also does nothing for a deferred transaction that has already read and then tries to write while another connection holds the write lock: SQLite skips the handler there and returns `SQLITE_BUSY` immediately, which is why such transactions should start with `BEGIN IMMEDIATE`. And it cannot help a reader blocked by a connection that holds the database in exclusive locking mode for as long as it stays open.

  • In SQLite, what is the difference between SQLITE_BUSY and SQLITE_LOCKED?
    `SQLITE_BUSY` reports a conflict with a different database connection, usually in another thread or process, such as a second writer. `SQLITE_LOCKED` reports a conflict within the same connection, or with a connection that shares its cache: for example `DROP TABLE` while that connection is still reading the table. A busy timeout addresses the first; the second is a bug in how one connection's statements are ordered.
  • Why might a SQLite connection get SQLITE_BUSY immediately even with busy_timeout set to 5000?
    Usually because it is upgrading: a deferred transaction that has already read cannot wait for another connection's write lock, so SQLite skips the handler. In WAL mode the same upgrade fails with `SQLITE_BUSY_SNAPSHOT` once another connection has committed. Other causes are a timeout set on a different connection than the one failing, or a custom busy handler that replaced it.
  • In SQLite, can COMMIT fail with SQLITE_BUSY, and what state is the transaction in afterwards?
    Yes, in rollback-journal mode: the commit must take `EXCLUSIVE`, which waits for other connections to drop their `SHARED` locks. If they outlast the busy handler, `COMMIT` returns `SQLITE_BUSY` but the transaction remains active, so the application can retry the `COMMIT` once the readers have cleared, or `ROLLBACK`.

  • SQLITE_BUSY means the database file is corrupt and needs repair.
  • SQLite waits for locks by default, so no timeout needs setting.
  • busy_timeout is saved in the database file once set.
  • A connection can run busy_timeout and a custom busy handler together.
  • SQLITE_BUSY and SQLITE_LOCKED are two names for the same error.