diff options
author | furszy <matiasfurszyfer@protonmail.com> | 2024-01-15 20:18:34 -0300 |
---|---|---|
committer | furszy <matiasfurszyfer@protonmail.com> | 2024-01-30 17:27:20 -0300 |
commit | 472d2ca98170049e0edec830e2d11c5ef23740a4 (patch) | |
tree | 486a9314ebc07c92d7184556b349242ea83d1ca8 /src/wallet/sqlite.cpp | |
parent | dca874e838c2599bd24433675b291168f8e7b055 (diff) | |
download | bitcoin-472d2ca98170049e0edec830e2d11c5ef23740a4.tar.xz |
sqlite: introduce HasActiveTxn method
Util function to clean up code and let us
verify, in the following-up commit, that dangling,
to-be-reverted db transactions cannot occur anymore.
Diffstat (limited to 'src/wallet/sqlite.cpp')
-rw-r--r-- | src/wallet/sqlite.cpp | 16 |
1 files changed, 11 insertions, 5 deletions
diff --git a/src/wallet/sqlite.cpp b/src/wallet/sqlite.cpp index bcb77c18f7..89bb917b52 100644 --- a/src/wallet/sqlite.cpp +++ b/src/wallet/sqlite.cpp @@ -377,6 +377,12 @@ void SQLiteDatabase::Close() m_db = nullptr; } +bool SQLiteDatabase::HasActiveTxn() +{ + // 'sqlite3_get_autocommit' returns true by default, and false if a transaction has begun and not been committed or rolled back. + return m_db && sqlite3_get_autocommit(m_db) == 0; +} + int SQliteExecHandler::Exec(SQLiteDatabase& database, const std::string& statement) { return sqlite3_exec(database.m_db, statement.data(), nullptr, nullptr, nullptr); @@ -399,8 +405,8 @@ SQLiteBatch::SQLiteBatch(SQLiteDatabase& database) void SQLiteBatch::Close() { - // If m_db is in a transaction (i.e. not in autocommit mode), then abort the transaction in progress - if (m_database.m_db && sqlite3_get_autocommit(m_database.m_db) == 0) { + // If we began a transaction, and it wasn't committed, abort the transaction in progress + if (m_database.HasActiveTxn()) { if (TxnAbort()) { LogPrintf("SQLiteBatch: Batch closed unexpectedly without the transaction being explicitly committed or aborted\n"); } else { @@ -611,7 +617,7 @@ std::unique_ptr<DatabaseCursor> SQLiteBatch::GetNewPrefixCursor(Span<const std:: bool SQLiteBatch::TxnBegin() { - if (!m_database.m_db || sqlite3_get_autocommit(m_database.m_db) == 0) return false; + if (!m_database.m_db || m_database.HasActiveTxn()) return false; int res = Assert(m_exec_handler)->Exec(m_database, "BEGIN TRANSACTION"); if (res != SQLITE_OK) { LogPrintf("SQLiteBatch: Failed to begin the transaction\n"); @@ -621,7 +627,7 @@ bool SQLiteBatch::TxnBegin() bool SQLiteBatch::TxnCommit() { - if (!m_database.m_db || sqlite3_get_autocommit(m_database.m_db) != 0) return false; + if (!m_database.HasActiveTxn()) return false; int res = Assert(m_exec_handler)->Exec(m_database, "COMMIT TRANSACTION"); if (res != SQLITE_OK) { LogPrintf("SQLiteBatch: Failed to commit the transaction\n"); @@ -631,7 +637,7 @@ bool SQLiteBatch::TxnCommit() bool SQLiteBatch::TxnAbort() { - if (!m_database.m_db || sqlite3_get_autocommit(m_database.m_db) != 0) return false; + if (!m_database.HasActiveTxn()) return false; int res = Assert(m_exec_handler)->Exec(m_database, "ROLLBACK TRANSACTION"); if (res != SQLITE_OK) { LogPrintf("SQLiteBatch: Failed to abort the transaction\n"); |