From c2fb07c2f40729fbd890485801d61fe888e6e507 Mon Sep 17 00:00:00 2001 From: 0x1a8510f2 Date: Thu, 20 Oct 2022 21:43:50 +0100 Subject: [PATCH] Fix database is locked errors --- internal/sqlutil/sqlutil.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/internal/sqlutil/sqlutil.go b/internal/sqlutil/sqlutil.go index 93f24b070..b0c339294 100644 --- a/internal/sqlutil/sqlutil.go +++ b/internal/sqlutil/sqlutil.go @@ -5,6 +5,7 @@ import ( "flag" "fmt" "regexp" + "strings" "github.com/matrix-org/dendrite/setup/config" "github.com/sirupsen/logrus" @@ -25,6 +26,17 @@ func Open(dbProperties *config.DatabaseOptions, writer Writer) (*sql.DB, error) if err != nil { return nil, fmt.Errorf("ParseFileURI: %w", err) } + + // add query parameters to the dsn + if strings.Contains(dsn, "?") { + dsn += "&" + } else { + dsn += "?" + } + + // wait some time before erroring if the db is locked + // https://gitlab.com/cznic/sqlite/-/issues/106#note_1058094993 + dsn += "_pragma=busy_timeout%3d10000" case dbProperties.ConnectionString.IsPostgres(): driverName = "postgres" dsn = string(dbProperties.ConnectionString)