Add another try/catch

This commit is contained in:
Simon Quigley 2025-01-27 12:58:31 -06:00
parent 2bc5c191f0
commit fb1d5e4340

View File

@ -46,23 +46,28 @@ QSqlDatabase get_thread_connection() {
// Check if the connection already exists for this thread // Check if the connection already exists for this thread
attempt++; attempt++;
if (QSqlDatabase::contains(connection_name)) { try {
QSqlDatabase db = QSqlDatabase::database(connection_name); if (QSqlDatabase::contains(connection_name)) {
if (!db.isOpen()) { QSqlDatabase db = QSqlDatabase::database(connection_name);
if (!db.open()) { if (!db.isOpen()) {
std::string last_error_text = db.lastError().text().toStdString(); if (!db.open()) {
if (last_error_text.contains("unable to open database file")) { std::string last_error_text = db.lastError().text().toStdString();
std::this_thread::sleep_for(std::chrono::milliseconds(get_delay(attempt))); if (last_error_text.contains("unable to open database file")) {
continue; std::this_thread::sleep_for(std::chrono::milliseconds(get_delay(attempt)));
continue;
}
throw std::runtime_error(std::format("Failed to open thread-specific database connection: {}", last_error_text));
} }
throw std::runtime_error(std::format("Failed to open thread-specific database connection: {}", last_error_text));
} }
return db;
} }
return db;
}
thread_db = QSqlDatabase::addDatabase("QSQLITE", connection_name); thread_db = QSqlDatabase::addDatabase("QSQLITE", connection_name);
thread_db.setDatabaseName(shared_database_path); thread_db.setDatabaseName(shared_database_path);
} catch (...) {
std::this_thread::sleep_for(std::chrono::milliseconds(get_delay(attempt)));
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()) throw std::runtime_error("Failed to open new database connection for thread: " + thread_db.lastError().text().toStdString());
passed = true; passed = true;