Further refinements to thread_db initialization

main
Simon Quigley 2 weeks ago
parent 3bff0f3651
commit b94ec17fd3

@ -41,19 +41,23 @@ QSqlDatabase get_thread_connection() {
while (!passed) { while (!passed) {
QSqlDatabase thread_db; QSqlDatabase thread_db;
{
std::lock_guard<std::mutex> lock(connection_mutex_); std::lock_guard<std::mutex> lock(connection_mutex_);
thread_local unsigned int thread_unique_id = thread_id_counter.fetch_add(1); thread_local unsigned int thread_unique_id = thread_id_counter.fetch_add(1);
connection_name = QString("CIConn_%1").arg(thread_unique_id); connection_name = QString("CIConn_%1").arg(thread_unique_id);
attempt++;
}
// Check if the connection already exists for this thread // Check if the connection already exists for this thread
attempt++;
try { try {
if (QSqlDatabase::contains(connection_name)) { if (QSqlDatabase::contains(connection_name)) {
QSqlDatabase db = QSqlDatabase::database(connection_name); QSqlDatabase db = QSqlDatabase::database(connection_name);
db.setConnectOptions(QStringLiteral("QSQLITE_BUSY_TIMEOUT=5000"));
if (!db.isOpen()) { if (!db.isOpen()) {
if (!db.open()) { if (!db.open()) {
std::string last_error_text = db.lastError().text().toStdString(); std::string last_error_text = db.lastError().text().toStdString();
if (last_error_text.contains("unable to open database file")) { if (last_error_text.contains("unable to open database file") |
last_error_text.contains("database is locked")) {
std::this_thread::sleep_for(std::chrono::milliseconds(get_delay(attempt))); std::this_thread::sleep_for(std::chrono::milliseconds(get_delay(attempt)));
continue; continue;
} }
@ -70,7 +74,15 @@ QSqlDatabase get_thread_connection() {
continue; continue;
} }
if (!thread_db.open()) throw std::runtime_error("Failed to open new database connection for thread: " + thread_db.lastError().text().toStdString()); if (!thread_db.open()) {
const QString err = thread_db.lastError().text();
if (err.contains("unable to open database file") || err.contains("database is locked"))
{
std::this_thread::sleep_for(std::chrono::milliseconds(get_delay(attempt)));
continue;
}
throw std::runtime_error("Failed to open new database connection: " + err.toStdString());
}
return thread_db; return thread_db;
} }
} }

Loading…
Cancel
Save