diff --git a/lib/data/sqlite/sqlite_class.cpp b/lib/data/sqlite/sqlite_class.cpp index 3b5b0363..509d79b4 100644 --- a/lib/data/sqlite/sqlite_class.cpp +++ b/lib/data/sqlite/sqlite_class.cpp @@ -117,6 +117,7 @@ SQLite_Connector::SQLite_Connector() m_bIsConnected = false; m_bPersistentConnection = false; m_pDBLink = NULL; + m_nBusyTimeoutMs = SQLITE_BUSY_TIMEOUT_MS; if (! sqlite_global_config_is_set) { // if( SQLITE_OK != sqlite3_config(int, ...) ){ SQLITE_MISUSE @@ -166,8 +167,7 @@ bool SQLite_Connector::Connect(CSTR FileName, int AUX_FLAGS, bool Persistent) #endif if (SQLITE_OK == sqlite3_open_v2(m_pDBName, &m_pDBLink, m_nFLAGS, NULL)) { - // Wait for locks held by other connections instead of failing immediately - sqlite3_busy_timeout(m_pDBLink, SQLITE_BUSY_TIMEOUT_MS); + applyBusyTimeout(); m_bIsConnected = true; } else if (m_pDBLink != NULL) @@ -188,6 +188,23 @@ bool SQLite_Connector::Connect(CSTR FileName, int AUX_FLAGS, bool Persistent) return m_bIsConnected; } +void SQLite_Connector::applyBusyTimeout(void) +{ + if (m_pDBLink != NULL) + { + // <= 0 removes the busy handler: a locked database fails with SQLITE_BUSY at once + sqlite3_busy_timeout(m_pDBLink, m_nBusyTimeoutMs > 0 ? m_nBusyTimeoutMs : 0); + }; +} + +void SQLite_Connector::setBusyTimeout(int ms) +{ + m_mAccessMutex.lock(); + m_nBusyTimeoutMs = ms > 0 ? ms : 0; + applyBusyTimeout(); // a persistent link is open already; non-persistent ones get it on ReConnect() + m_mAccessMutex.unlock(); +} + bool SQLite_Connector::ReConnect(void) { if (m_pDBLink != NULL) @@ -200,8 +217,7 @@ bool SQLite_Connector::ReConnect(void) } if (SQLITE_OK == sqlite3_open_v2(m_pDBName, &m_pDBLink, m_nFLAGS, NULL)) { - // Wait for locks held by other connections instead of failing immediately - sqlite3_busy_timeout(m_pDBLink, SQLITE_BUSY_TIMEOUT_MS); + applyBusyTimeout(); return true; } if (m_pDBLink != NULL) @@ -252,8 +268,15 @@ bool SQLite_Connector::SilentQuery(CSTR QS) LOG(sqlite3_expanded_sql(query)); #endif - if (SQLITE_DONE != sqlite3_step(query)) + int stepRC = sqlite3_step(query); + if (SQLITE_DONE != stepRC) { + if (SQLITE_ROW != stepRC) + { + char tB[256]; + snprintf(tB, sizeof(tB), "SilentQuery step failed (%d): %.200s", stepRC, sqlite3_errmsg(m_pDBLink)); + LOG_ERROR(tB); + }; sqlite3_finalize(query); m_mAccessMutex.unlock(); return false; @@ -338,7 +361,7 @@ AAssoc * SQLite_Connector::Query(CSTR QS, bool useCache) if (stepRC != SQLITE_DONE) { char tB[256]; - snprintf(tB, sizeof(tB), "Query step failed (%d): %s", stepRC, sqlite3_errmsg(m_pDBLink)); + snprintf(tB, sizeof(tB), "Query step failed (%d): %.200s", stepRC, sqlite3_errmsg(m_pDBLink)); LOG_ERROR(tB); }; diff --git a/lib/data/sqlite/sqlite_class.h b/lib/data/sqlite/sqlite_class.h index adf0a76f..ecbbada0 100644 --- a/lib/data/sqlite/sqlite_class.h +++ b/lib/data/sqlite/sqlite_class.h @@ -8,8 +8,9 @@ #include #ifndef SQLITE_BUSY_TIMEOUT_MS - // How long a statement waits for a lock held by another connection before failing with SQLITE_BUSY - #define SQLITE_BUSY_TIMEOUT_MS 2000 + // Default time a statement waits for a lock held by another connection before failing with + // SQLITE_BUSY. 0 = no waiting: fail at once (callers must not stall). Per connection: setBusyTimeout() + #define SQLITE_BUSY_TIMEOUT_MS 0 #endif #ifdef DEBUG_EXTRA @@ -29,6 +30,9 @@ class SQLite_Connector char m_pDBName[MAX_PATH]; int m_nFLAGS; bool m_bPersistentConnection; + int m_nBusyTimeoutMs; + + void applyBusyTimeout(void); public: @@ -42,6 +46,9 @@ class SQLite_Connector CSTR escapeString(CSTR str); CSTR escapeBinary(uint8_t * Data, uint32_t Size); + // Wait up to ms for a locked database before failing with SQLITE_BUSY (0 = fail at once) + void setBusyTimeout(int ms); + bool isConnected(void) { return m_bIsConnected;