Skip to content

Commit

Permalink
Fix database locked errors with SQLite
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
mheon committed Mar 21, 2023
1 parent 94f905a commit 9f0e0e8
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion libpod/sqlite_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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 {
Expand Down

0 comments on commit 9f0e0e8

Please sign in to comment.