From c5f4fd23fc8358f8a3f5c1649b1a00c14bb4eb57 Mon Sep 17 00:00:00 2001 From: Bart De Vries Date: Mon, 27 Feb 2023 21:07:11 +0100 Subject: [PATCH] Set database to WAL mode on startup Using WAL mode avoids having to lock the database for certain transactions. Therefore, deadlocks between read/writes from different threads should not happen anymore. These were rare, but happened sometimes on slower hardware or slow storage devices. BUG: 465110 --- src/database.cpp | 22 ++++++++++++++++++++++ src/database.h | 1 + 2 files changed, 23 insertions(+) diff --git a/src/database.cpp b/src/database.cpp index e45c97b7..99236a35 100644 --- a/src/database.cpp +++ b/src/database.cpp @@ -47,6 +47,8 @@ void Database::closeDatabase(const QString &connectionName) bool Database::migrate() { + setWalMode(); + int dbversion = version(); if (dbversion < 1) TRUE_OR_RETURN(migrateTo1()); @@ -200,6 +202,26 @@ int Database::version() return -1; } +void Database::setWalMode() +{ + bool ok = false; + QSqlQuery query; + query.prepare(QStringLiteral("PRAGMA journal_mode;")); + execute(query); + if (query.next()) { + ok = (query.value(0).toString() == QStringLiteral("wal")); + } + + if (!ok) { + query.prepare(QStringLiteral("PRAGMA journal_mode=WAL;")); + execute(query); + if (query.next()) { + ok = (query.value(0).toString() == QStringLiteral("wal")); + } + qDebug() << "Activating WAL mode on database:" << (ok ? "ok" : "not ok!"); + } +} + void Database::cleanup() { // TODO: create database sanity checks, or, alternatively, create database scrub routine diff --git a/src/database.h b/src/database.h index 68170b14..5cd745c4 100644 --- a/src/database.h +++ b/src/database.h @@ -43,6 +43,7 @@ private: bool migrateTo5(); bool migrateTo6(); void cleanup(); + void setWalMode(); inline static const QString m_dbName = QStringLiteral("database.db3"); };