From aadc47956edea63776c2d4466d6c3ee0557c0e43 Mon Sep 17 00:00:00 2001 From: Paul Oliver Date: Wed, 26 Nov 2025 02:20:36 +0100 Subject: Enable SQLite Write-Ahead Logging (WAL) --- core.j2.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'core.j2.c') diff --git a/core.j2.c b/core.j2.c index 45cd8b8..bd4b266 100644 --- a/core.j2.c +++ b/core.j2.c @@ -609,6 +609,31 @@ void salis_auto_save() { } {% endif %} +{% if data_push_path is defined %} +void salis_exec_sql(const char *sql) { + assert(sql); + + int sql_res; + char *sql_err; + + while ((sql_res = sqlite3_exec(g_sim_data, sql, NULL, NULL, &sql_err)) == SQLITE_BUSY) { + g_warn("SQLite database returned error '%d' with message:", sql_res); + g_warn(sql_err); + sqlite3_free(sql_err); + + switch (sql_res) { + case SQLITE_BUSY: + // Only handle busy database errors + g_info("Will retry query..."); + continue; + default: + // Application should fail on all other error conditions + assert(false); + } + } +} +{% endif %} + {% if args.command in ["bench", "new"] %} void salis_init() { assert(g_info); @@ -631,6 +656,11 @@ void salis_init() { // Install busy handler to retry transactions if DB is locked sqlite3_busy_timeout(g_sim_data, {{ data_push_busy_timeout }}); + // Enable Write-Ahead Logging (WAL) + // This seems to help prevent DB locks when displaying live data + // See: https://sqlite.org/wal.html + salis_exec_sql("pragma journal_mode=wal;"); + arch_push_data_header(); arch_push_data_line(); {% endif %} -- cgit v1.2.1