[core] Disable SQLite busy timeouts so that multiple readers can access the same database
This commit is contained in:
@@ -37,6 +37,7 @@ void OfflineDatabase::ensureSchema() {
|
||||
if (path != ":memory:") {
|
||||
try {
|
||||
db = std::make_unique<Database>(path.c_str(), ReadWrite);
|
||||
db->setBusyTimeout(Milliseconds::max());
|
||||
|
||||
{
|
||||
Statement userVersionStmt(db->prepare("PRAGMA user_version"));
|
||||
@@ -51,12 +52,15 @@ void OfflineDatabase::ensureSchema() {
|
||||
|
||||
removeExisting();
|
||||
db = std::make_unique<Database>(path.c_str(), ReadWrite | Create);
|
||||
db->setBusyTimeout(Milliseconds::max());
|
||||
} catch (mapbox::sqlite::Exception& ex) {
|
||||
if (ex.code == SQLITE_CANTOPEN) {
|
||||
db = std::make_unique<Database>(path.c_str(), ReadWrite | Create);
|
||||
db->setBusyTimeout(Milliseconds::max());
|
||||
} else if (ex.code == SQLITE_NOTADB) {
|
||||
removeExisting();
|
||||
db = std::make_unique<Database>(path.c_str(), ReadWrite | Create);
|
||||
db->setBusyTimeout(Milliseconds::max());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -64,6 +68,7 @@ void OfflineDatabase::ensureSchema() {
|
||||
#include "offline_schema.cpp.include"
|
||||
|
||||
db = std::make_unique<Database>(path.c_str(), ReadWrite | Create);
|
||||
db->setBusyTimeout(Milliseconds::max());
|
||||
db->exec(schema);
|
||||
db->exec("PRAGMA user_version = " + util::toString(schemaVersion));
|
||||
}
|
||||
|
||||
@@ -59,6 +59,15 @@ Database::operator bool() const {
|
||||
return db != nullptr;
|
||||
}
|
||||
|
||||
void Database::setBusyTimeout(std::chrono::milliseconds timeout) {
|
||||
assert(db);
|
||||
const int err = sqlite3_busy_timeout(db,
|
||||
int(std::min<std::chrono::milliseconds::rep>(timeout.count(), std::numeric_limits<int>::max())));
|
||||
if (err != SQLITE_OK) {
|
||||
throw Exception { err, sqlite3_errmsg(db) };
|
||||
}
|
||||
}
|
||||
|
||||
void Database::exec(const std::string &sql) {
|
||||
assert(db);
|
||||
char *msg = nullptr;
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <stdexcept>
|
||||
#include <chrono>
|
||||
|
||||
typedef struct sqlite3 sqlite3;
|
||||
typedef struct sqlite3_stmt sqlite3_stmt;
|
||||
@@ -41,6 +42,7 @@ public:
|
||||
|
||||
explicit operator bool() const;
|
||||
|
||||
void setBusyTimeout(std::chrono::milliseconds);
|
||||
void exec(const std::string &sql);
|
||||
Statement prepare(const char *query);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user