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
This commit is contained in:
Bart De Vries 2023-02-27 21:07:11 +01:00
parent 6e998d608c
commit c5f4fd23fc
2 changed files with 23 additions and 0 deletions

View File

@ -47,6 +47,8 @@ void Database::closeDatabase(const QString &connectionName)
bool Database::migrate() bool Database::migrate()
{ {
setWalMode();
int dbversion = version(); int dbversion = version();
if (dbversion < 1) if (dbversion < 1)
TRUE_OR_RETURN(migrateTo1()); TRUE_OR_RETURN(migrateTo1());
@ -200,6 +202,26 @@ int Database::version()
return -1; 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() void Database::cleanup()
{ {
// TODO: create database sanity checks, or, alternatively, create database scrub routine // TODO: create database sanity checks, or, alternatively, create database scrub routine

View File

@ -43,6 +43,7 @@ private:
bool migrateTo5(); bool migrateTo5();
bool migrateTo6(); bool migrateTo6();
void cleanup(); void cleanup();
void setWalMode();
inline static const QString m_dbName = QStringLiteral("database.db3"); inline static const QString m_dbName = QStringLiteral("database.db3");
}; };