fix(db): enable WAL + busy_timeout on SQLite connections (#2917)

Uploads and cron jobs intermittently failed with "database is locked"
(SQLITE_BUSY) until restart. The custom sqlite3 driver only set
foreign_keys; it now also enables WAL, synchronous=NORMAL, and a 5s
busy_timeout on every new connection so cross-connection contention
waits instead of erroring instantly.

MaxOpenConns stays at 1: a single connection serializes statements,
which avoids same-connection read-cursor/write conflicts that
busy_timeout cannot retry.

Authored By: TDvorak <info@tdvorak.dev>

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
pull/3582/head
Tomas Dvorak 2 weeks ago
parent 04583175bb
commit 881a9347e8

@ -149,9 +149,18 @@ func (d sqlite3Driver) Open(name string) (conn driver.Conn, err error) {
if err != nil { if err != nil {
return return
} }
_, err = conn.(sqlite3DriverConn).Exec("PRAGMA foreign_keys = ON;", nil) // WAL lets readers coexist with the writer; busy_timeout makes a brief
if err != nil { // lock wait instead of failing instantly with SQLITE_BUSY (#2917).
for _, pragma := range []string{
"PRAGMA journal_mode = WAL;",
"PRAGMA synchronous = NORMAL;",
"PRAGMA busy_timeout = 5000;",
"PRAGMA foreign_keys = ON;",
} {
if _, err = conn.(sqlite3DriverConn).Exec(pragma, nil); err != nil {
_ = conn.Close() _ = conn.Close()
return
}
} }
return return
} }

Loading…
Cancel
Save