diff --git a/src/Server/Helper/SQLHelper.cs b/src/Server/Helper/SQLHelper.cs index efb24aa..c1dc295 100644 --- a/src/Server/Helper/SQLHelper.cs +++ b/src/Server/Helper/SQLHelper.cs @@ -1,3 +1,4 @@ +using System.Data; using System.Data.Common; using MySql.Data.MySqlClient; @@ -6,6 +7,7 @@ namespace Server.Helper; public class SQLHelper:IDisposable { public MySqlConnection connection; + public DbDataReader? dbDataReader; public string connectionString; public SQLHelper(MySqlConnection connection, string connectionString) { @@ -30,13 +32,15 @@ public class SQLHelper:IDisposable lock (connection) { EnsureConnected(); + EnsureDbReaderIsClosed(); using MySqlCommand command = connection.CreateCommand(); command.CommandText = query; foreach (KeyValuePair parameter in parameters) { command.Parameters.AddWithValue($"@{parameter.Key}", parameter.Value); } - return command.ExecuteReader(); + dbDataReader = command.ExecuteReader(); + return dbDataReader; } } @@ -45,6 +49,7 @@ public class SQLHelper:IDisposable lock (connection) { EnsureConnected(); + EnsureDbReaderIsClosed(); using MySqlCommand command = connection.CreateCommand(); command.CommandText = query; @@ -61,6 +66,7 @@ public class SQLHelper:IDisposable lock (connection) { EnsureConnected(); + EnsureDbReaderIsClosed(); using MySqlCommand command = connection.CreateCommand(); command.CommandText = query; @@ -83,11 +89,29 @@ public class SQLHelper:IDisposable connection.Close(); connection.Open(); } - catch (Exception) + catch (Exception ex) { - throw; // TODO add logging here + ElmahCore.ElmahExtensions.RaiseError(ex); + throw; } } return true; } + + public void EnsureDbReaderIsClosed() + { + int counter = 0; + int sleepTime = 10; + int timeout = 5000; + while (!(dbDataReader?.IsClosed ?? true)) + { + if (counter > timeout / sleepTime) + { + TimeoutException ex = new("Unable to ensure dbDataReader is closed"); + ElmahCore.ElmahExtensions.RaiseError(ex); + throw ex; + } + Thread.Sleep(sleepTime); + } + } } \ No newline at end of file diff --git a/src/Server/Helper/SearchdomainHelper.cs b/src/Server/Helper/SearchdomainHelper.cs index 5b4d256..9be053b 100644 --- a/src/Server/Helper/SearchdomainHelper.cs +++ b/src/Server/Helper/SearchdomainHelper.cs @@ -88,7 +88,7 @@ public class SearchdomainHelper(ILogger logger, DatabaseHelp public Entity? EntityFromJSON(SearchdomainManager searchdomainManager, ILogger logger, JSONEntity jsonEntity) //string json) { - SQLHelper helper = searchdomainManager.helper.DuplicateConnection(); + using SQLHelper helper = searchdomainManager.helper.DuplicateConnection(); Searchdomain searchdomain = searchdomainManager.GetSearchdomain(jsonEntity.Searchdomain); List entityCache = searchdomain.entityCache; AIProvider aIProvider = searchdomain.aIProvider; diff --git a/src/Server/SearchdomainManager.cs b/src/Server/SearchdomainManager.cs index dbc45af..9cc5019 100644 --- a/src/Server/SearchdomainManager.cs +++ b/src/Server/SearchdomainManager.cs @@ -82,12 +82,18 @@ public class SearchdomainManager { DbDataReader reader = helper.ExecuteSQLCommand("SELECT name FROM searchdomain", []); List results = []; - while (reader.Read()) + try { - results.Add(reader.GetString(0)); + while (reader.Read()) + { + results.Add(reader.GetString(0)); + } + return results; + } + finally + { + reader.Close(); } - reader.Close(); - return results; } }