SQLite: fail at once on a locked database again (no 2 s busy wait) #21

Merged
ClaudeAI merged 1 commits from fix/sqlite-no-busy-wait into master 1 hour ago
  1. 35
      lib/data/sqlite/sqlite_class.cpp
  2. 11
      lib/data/sqlite/sqlite_class.h

35
lib/data/sqlite/sqlite_class.cpp

@ -117,6 +117,7 @@ SQLite_Connector::SQLite_Connector()
m_bIsConnected = false; m_bIsConnected = false;
m_bPersistentConnection = false; m_bPersistentConnection = false;
m_pDBLink = NULL; m_pDBLink = NULL;
m_nBusyTimeoutMs = SQLITE_BUSY_TIMEOUT_MS;
if (! sqlite_global_config_is_set) if (! sqlite_global_config_is_set)
{ {
// if( SQLITE_OK != sqlite3_config(int, ...) ){ SQLITE_MISUSE // if( SQLITE_OK != sqlite3_config(int, ...) ){ SQLITE_MISUSE
@ -166,8 +167,7 @@ bool SQLite_Connector::Connect(CSTR FileName, int AUX_FLAGS, bool Persistent)
#endif #endif
if (SQLITE_OK == sqlite3_open_v2(m_pDBName, &m_pDBLink, m_nFLAGS, NULL)) 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; m_bIsConnected = true;
} }
else if (m_pDBLink != NULL) else if (m_pDBLink != NULL)
@ -188,6 +188,23 @@ bool SQLite_Connector::Connect(CSTR FileName, int AUX_FLAGS, bool Persistent)
return m_bIsConnected; 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) bool SQLite_Connector::ReConnect(void)
{ {
if (m_pDBLink != NULL) 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)) 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; return true;
} }
if (m_pDBLink != NULL) if (m_pDBLink != NULL)
@ -252,8 +268,15 @@ bool SQLite_Connector::SilentQuery(CSTR QS)
LOG(sqlite3_expanded_sql(query)); LOG(sqlite3_expanded_sql(query));
#endif #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); sqlite3_finalize(query);
m_mAccessMutex.unlock(); m_mAccessMutex.unlock();
return false; return false;
@ -338,7 +361,7 @@ AAssoc * SQLite_Connector::Query(CSTR QS, bool useCache)
if (stepRC != SQLITE_DONE) if (stepRC != SQLITE_DONE)
{ {
char tB[256]; 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); LOG_ERROR(tB);
}; };

11
lib/data/sqlite/sqlite_class.h

@ -8,8 +8,9 @@
#include <sqlite3.h> #include <sqlite3.h>
#ifndef SQLITE_BUSY_TIMEOUT_MS #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 #endif
#ifdef DEBUG_EXTRA #ifdef DEBUG_EXTRA
@ -29,6 +30,9 @@ class SQLite_Connector
char m_pDBName[MAX_PATH]; char m_pDBName[MAX_PATH];
int m_nFLAGS; int m_nFLAGS;
bool m_bPersistentConnection; bool m_bPersistentConnection;
int m_nBusyTimeoutMs;
void applyBusyTimeout(void);
public: public:
@ -42,6 +46,9 @@ class SQLite_Connector
CSTR escapeString(CSTR str); CSTR escapeString(CSTR str);
CSTR escapeBinary(uint8_t * Data, uint32_t Size); 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) bool isConnected(void)
{ {
return m_bIsConnected; return m_bIsConnected;

Loading…
Cancel
Save