From ec00b69a714fa91658abf7fc1cd78d9738daa2be Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 17 May 2025 00:23:00 +0200 Subject: [PATCH] Amendments to the parent commit, changing SQLite timeout setting This should have been part of the parent commit but got accidentally left out. See #1254. --- src/backends/sqlite3/session.cpp | 2 +- tests/sqlite3/test-sqlite3.cpp | 46 +++++++++++++++++++++++++++++++- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/src/backends/sqlite3/session.cpp b/src/backends/sqlite3/session.cpp index 9d96d7e4..93a46ae3 100644 --- a/src/backends/sqlite3/session.cpp +++ b/src/backends/sqlite3/session.cpp @@ -173,7 +173,7 @@ sqlite3_session_backend::sqlite3_session_backend( } ); - //Set the timeout first to have effect on the following queries. + // Set the timeout first to have effect on the following queries. res = sqlite3_busy_timeout(conn_, timeout * 1000); check_sqlite_err(conn_, res, "Failed to set busy timeout for connection. "); diff --git a/tests/sqlite3/test-sqlite3.cpp b/tests/sqlite3/test-sqlite3.cpp index b5af0709..f3255528 100644 --- a/tests/sqlite3/test-sqlite3.cpp +++ b/tests/sqlite3/test-sqlite3.cpp @@ -11,8 +11,11 @@ #include -#include #include +#include +#include +#include +#include using namespace soci; using namespace soci::tests; @@ -934,6 +937,47 @@ TEST_CASE("SQLite row int64", "[sqlite][row][int64]") CHECK(r.get("val") == val); } +// The setting "synchronous" cannot be set when the database is locked in +// environments with parallelisms for example. A timeout solves this issue. +// This test checks whether the timeout was applied before setting "synchronous". +TEST_CASE("SQLite synchronous option works from multiple threads", + "[sqlite][pragma]") +{ + { + soci::session sql(backEnd, "db=test.db"); + sql << R"(PRAGMA journal_mode="WAL")"; + } + + struct FileRemover + { + // Argument must be a literal string as we just keep the pointer. + explicit FileRemover(char const* fileName) : fileName_(fileName) {} + ~FileRemover() { std::remove(fileName_); } + + char const* const fileName_; + }; + + FileRemover fileRemoverDB("test.db"); + FileRemover fileRemoverSHM("test.db-shm"); + FileRemover fileRemoverWAL("test.db-wal"); + + std::forward_list threads; + for (int i = 0; i < 32; ++i) + { + threads.emplace_front([]() -> void + { + REQUIRE_NOTHROW( + soci::session(backEnd, "db=test.db synchronous=extra timeout=2") + ); + }); + } + + for (auto& thr : threads) + { + thr.join(); + } +} + // DDL Creation objects for common tests struct table_creator_one : public table_creator_base {