Fix database locked errors with SQLite

I was searching the SQLite docs for a fix, but apparently that
was the wrong place; it's a common enough error with the Go
frontend for SQLite that the fix is prominently listed in the API
docs for go-sqlite3. Setting cache mode to 'shared' and using a
maximum of 1 simultaneous open connection should fix.

Performance implications of this are unclear, but cache=shared
sounds like it will be a benefit, not a curse.

[NO NEW TESTS NEEDED] This fixes a flake with concurrent DB
access.

Signed-off-by: Matthew Heon <matthew.heon@pm.me>
This commit is contained in:
Matthew Heon 2023-03-21 09:57:56 -04:00
parent 94f905a503
commit 9f0e0e8331
1 changed files with 4 additions and 1 deletions

View File

@ -45,7 +45,7 @@ func NewSqliteState(runtime *Runtime) (_ State, defErr error) {
return nil, fmt.Errorf("creating root directory: %w", err)
}
conn, err := sql.Open("sqlite3", filepath.Join(basePath, "db.sql?_loc=auto"))
conn, err := sql.Open("sqlite3", filepath.Join(basePath, "db.sql?_loc=auto&cache=shared"))
if err != nil {
return nil, fmt.Errorf("initializing sqlite database: %w", err)
}
@ -57,6 +57,9 @@ func NewSqliteState(runtime *Runtime) (_ State, defErr error) {
}
}()
// Necessary to avoid database locked errors.
conn.SetMaxOpenConns(1)
state.conn = conn
if err := state.conn.Ping(); err != nil {