Skip to content

Commit

Permalink
wallet: sqlite: there is no need to have exclusive locking when mocking
Browse files Browse the repository at this point in the history
For in-memory SQLite databases, exclusive locking is generally not
necessary because in-memory dbs are private to the connection that
created them.
  • Loading branch information
brunoerg committed Dec 10, 2024
1 parent 43cec44 commit 43d101f
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions src/wallet/sqlite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,15 +278,18 @@ void SQLiteDatabase::Open()

// Acquire an exclusive lock on the database
// First change the locking mode to exclusive
SetPragma(m_db, "locking_mode", "exclusive", "Unable to change database locking mode to exclusive");
// Now begin a transaction to acquire the exclusive lock. This lock won't be released until we close because of the exclusive locking mode.
int ret = sqlite3_exec(m_db, "BEGIN EXCLUSIVE TRANSACTION", nullptr, nullptr, nullptr);
if (ret != SQLITE_OK) {
throw std::runtime_error("SQLiteDatabase: Unable to obtain an exclusive lock on the database, is it being used by another instance of " CLIENT_NAME "?\n");
}
ret = sqlite3_exec(m_db, "COMMIT", nullptr, nullptr, nullptr);
if (ret != SQLITE_OK) {
throw std::runtime_error(strprintf("SQLiteDatabase: Unable to end exclusive lock transaction: %s\n", sqlite3_errstr(ret)));
int ret;
if (!m_mock) {
SetPragma(m_db, "locking_mode", "exclusive", "Unable to change database locking mode to exclusive");
// Now begin a transaction to acquire the exclusive lock. This lock won't be released until we close because of the exclusive locking mode.
ret = sqlite3_exec(m_db, "BEGIN EXCLUSIVE TRANSACTION", nullptr, nullptr, nullptr);
if (ret != SQLITE_OK) {
throw std::runtime_error("SQLiteDatabase: Unable to obtain an exclusive lock on the database, is it being used by another instance of " CLIENT_NAME "?\n");
}
ret = sqlite3_exec(m_db, "COMMIT", nullptr, nullptr, nullptr);
if (ret != SQLITE_OK) {
throw std::runtime_error(strprintf("SQLiteDatabase: Unable to end exclusive lock transaction: %s\n", sqlite3_errstr(ret)));
}
}

// Enable fullfsync for the platforms that use it
Expand Down

0 comments on commit 43d101f

Please sign in to comment.