Skip to content

/errors/sqlite/database-is-locked

SQLite: database is locked (SQLITE_BUSY)

verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash)updated 2026-10-20
tool
SQLite
exit code
0
versions
SQLite 3
os
any

SQLite allows many readers but only one writer at a time per database file. When a connection wants to write while another connection holds the write lock, SQLite returns SQLITE_BUSY, which drivers report as database is locked. With a busy timeout the second connection waits and retries; with a timeout of zero it fails at once.

Reproduce it

Two connections from Python's sqlite3 module; the first holds a write transaction, the second has timeout=0:

bash
python - <<'EOF'
import sqlite3
print("SQLite", sqlite3.sqlite_version)
a = sqlite3.connect("app.db", timeout=0)
a.execute("create table jobs (id integer primary key, state text)")
a.commit()
a.execute("begin immediate")
a.execute("insert into jobs (state) values ('new')")

b = sqlite3.connect("app.db", timeout=0)
try:
    b.execute("insert into jobs (state) values ('other')")
except sqlite3.OperationalError as e:
    print(f"{type(e).__name__}: {e} ({e.sqlite_errorname})")

a.commit()
b.execute("insert into jobs (state) values ('other')")   # works once the lock is released
b.commit()
print(b.execute("select count(*) from jobs").fetchone()[0], "rows")
EOF

Output

SQLite 3.53.1
OperationalError: database is locked (SQLITE_BUSY)
2 rows

Fixes

python
conn = sqlite3.connect("app.db", timeout=5)          # wait up to 5 s for the lock (Python's default is 5)
conn.execute("PRAGMA journal_mode=WAL")              # readers and one writer at the same time
with conn:                                            # commits, or rolls back on an exception
    conn.execute("insert into jobs (state) values (?)", ("new",))

Other drivers: PRAGMA busy_timeout = 5000; works in any connection, and most ORMs expose it as a connection option.

Sources