From 9e6a0b0453b331aeb6609d5fd2296c3bf16763ba Mon Sep 17 00:00:00 2001
From: Simon Quigley <simon@tsimonq2.net>
Date: Mon, 27 Jan 2025 17:51:52 -0600
Subject: [PATCH] Initialize thread_db within the loop

---
 cpp/db_common.cpp | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/cpp/db_common.cpp b/cpp/db_common.cpp
index 37b1bca..94346b2 100644
--- a/cpp/db_common.cpp
+++ b/cpp/db_common.cpp
@@ -36,10 +36,11 @@ static int get_delay(int attempt) {
 
 QSqlDatabase get_thread_connection() {
     QString connection_name;
-    QSqlDatabase thread_db;
     bool passed = false;
     int attempt = 0;
     while (!passed) {
+        QSqlDatabase thread_db;
+
         std::lock_guard<std::mutex> lock(connection_mutex_);
         thread_local unsigned int thread_unique_id = thread_id_counter.fetch_add(1);
         connection_name = QString("CIConn_%1").arg(thread_unique_id);
@@ -70,10 +71,8 @@ QSqlDatabase get_thread_connection() {
         }
 
         if (!thread_db.open()) throw std::runtime_error("Failed to open new database connection for thread: " + thread_db.lastError().text().toStdString());
-        passed = true;
+        return thread_db;
     }
-
-    return thread_db;
 }
 
 bool ci_query_exec(QSqlQuery* query, const QString query_string) {