diff options
| author | Marco Bubke <[email protected]> | 2021-03-16 11:48:12 +0100 |
|---|---|---|
| committer | Marco Bubke <[email protected]> | 2021-03-22 08:34:33 +0000 |
| commit | baab71c073f8cc2314ed4a06c207f1d28e06f912 (patch) | |
| tree | 8669798e34588662e93a641f851b725512447a7f /src/libs/sqlite/sqlitedatabasebackend.cpp | |
| parent | a6cfbf82ac1a746c9dd37cdf1bac86cf393369c8 (diff) | |
Sqlite: Add busy handler
A busy handler is much more flexible than a timeout. In most cases we can
remove the busy exception handling code.
Change-Id: I59666ccfbd380a341412e3d25e6716b07097bf3c
Reviewed-by: Thomas Hartmann <[email protected]>
Diffstat (limited to 'src/libs/sqlite/sqlitedatabasebackend.cpp')
| -rw-r--r-- | src/libs/sqlite/sqlitedatabasebackend.cpp | 37 |
1 files changed, 24 insertions, 13 deletions
diff --git a/src/libs/sqlite/sqlitedatabasebackend.cpp b/src/libs/sqlite/sqlitedatabasebackend.cpp index 20af7589dbd..46097406e8c 100644 --- a/src/libs/sqlite/sqlitedatabasebackend.cpp +++ b/src/libs/sqlite/sqlitedatabasebackend.cpp @@ -37,15 +37,24 @@ #include "sqlite3.h" +#include <chrono> +#include <thread> + extern "C" { int sqlite3_carray_init(sqlite3 *db, char **pzErrMsg, const sqlite3_api_routines *pApi); } namespace Sqlite { +using namespace std::literals; + DatabaseBackend::DatabaseBackend(Database &database) : m_database(database) , m_databaseHandle(nullptr) + , m_busyHandler([](int) { + std::this_thread::sleep_for(10ms); + return true; + }) { } @@ -197,26 +206,22 @@ void DatabaseBackend::closeWithoutException() } } -void DatabaseBackend::registerBusyHandler() +namespace { + +int busyHandlerCallback(void *userData, int counter) { - int resultCode = sqlite3_busy_handler(sqliteDatabaseHandle(), &busyHandlerCallback, nullptr); + auto &&busyHandler = *static_cast<DatabaseBackend::BusyHandler *>(userData); - checkIfBusyTimeoutWasSet(resultCode); + return busyHandler(counter); } -void DatabaseBackend::registerRankingFunction() -{ -} +} // namespace -int DatabaseBackend::busyHandlerCallback(void *, int counter) +void DatabaseBackend::registerBusyHandler() { - Q_UNUSED(counter) -#ifdef QT_DEBUG - //qWarning() << "Busy handler invoked" << counter << "times!"; -#endif - QThread::msleep(10); + int resultCode = sqlite3_busy_handler(sqliteDatabaseHandle(), &busyHandlerCallback, &m_busyHandler); - return true; + checkIfBusyTimeoutWasSet(resultCode); } void DatabaseBackend::checkForOpenDatabaseWhichCanBeClosed() @@ -416,6 +421,12 @@ void DatabaseBackend::resetUpdateHook() sqlite3_update_hook(m_databaseHandle, nullptr, nullptr); } +void DatabaseBackend::setBusyHandler(DatabaseBackend::BusyHandler &&busyHandler) +{ + m_busyHandler = std::move(busyHandler); + registerBusyHandler(); +} + void DatabaseBackend::throwExceptionStatic(const char *whatHasHappens) { throw Exception(whatHasHappens); |
