diff options
-rw-r--r-- | platform/default/mbgl/storage/offline_database.cpp | 5 | ||||
-rw-r--r-- | platform/default/sqlite3.cpp | 9 | ||||
-rw-r--r-- | platform/default/sqlite3.hpp | 2 |
3 files changed, 16 insertions, 0 deletions
diff --git a/platform/default/mbgl/storage/offline_database.cpp b/platform/default/mbgl/storage/offline_database.cpp index c6510157bc..fb0dae0b2b 100644 --- a/platform/default/mbgl/storage/offline_database.cpp +++ b/platform/default/mbgl/storage/offline_database.cpp @@ -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)); } diff --git a/platform/default/sqlite3.cpp b/platform/default/sqlite3.cpp index 2cc0e9f001..31b9d56566 100644 --- a/platform/default/sqlite3.cpp +++ b/platform/default/sqlite3.cpp @@ -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; diff --git a/platform/default/sqlite3.hpp b/platform/default/sqlite3.hpp index 56040af46e..9e511ebaf9 100644 --- a/platform/default/sqlite3.hpp +++ b/platform/default/sqlite3.hpp @@ -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); |