From d40221e7151d62c5cd80cf3ddde92f378f186439 Mon Sep 17 00:00:00 2001 From: Player Date: Thu, 11 Aug 2022 06:39:54 +0000 Subject: [PATCH] Add SQLite WAL and busy_timeout SQLite has rather unfortunate defaults for concurrent accesses, leading to database locked errors. https://github.com/matrix-org/dendrite/pull/1290 appears to try to fix some of this, but doesn't appear to cover a concurrent read+write and the errors still happened in large quantities when I joined the dendrite room. This PR enables WAL for concurrent read+write and much better performance. It also sets busy_timeout to 10 seconds to not immediately fail operations while the database is locked, providing a DB level solution to PR 1290's objective with additional coverage. I chose this particular location to add the two statements as they should apply to every SQLite DB connection. From a quick test it looks as if the db locked errors are gone with this change. --- internal/sqlutil/sqlutil.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/internal/sqlutil/sqlutil.go b/internal/sqlutil/sqlutil.go index 0cdae6d30..94dce1262 100644 --- a/internal/sqlutil/sqlutil.go +++ b/internal/sqlutil/sqlutil.go @@ -46,6 +46,9 @@ func Open(dbProperties *config.DatabaseOptions, writer Writer) (*sql.DB, error) db.SetMaxOpenConns(dbProperties.MaxOpenConns()) db.SetMaxIdleConns(dbProperties.MaxIdleConns()) db.SetConnMaxLifetime(dbProperties.ConnMaxLifetime()) + } else { + db.Exec("PRAGMA busy_timeout = 10000;") + db.Exec("PRAGMA journal_mode=WAL;") } return db, nil }