Skip to content

Commit 7e9c72b

Browse files
authored
Merge pull request #176 from PxTools/feature/db-connection-health-check
Added method to check if the connection to the configured database is ok
2 parents 3a669af + f9ccc6b commit 7e9c72b

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

PCAxis.Sql/ApiUtils/ApiUtilStatic.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Text.RegularExpressions;
44

5+
using PCAxis.Sql.DbClient;
56
using PCAxis.Sql.DbConfig;
67
using PCAxis.Sql.Models;
78
using PCAxis.Sql.Repositories;
@@ -91,6 +92,25 @@ public static List<string> GetTablesPublishedBetween(DateTime from, DateTime to)
9192
return TablesPublishedBetweenRepositoryStatic.GetTablesPublishedBetween(from, to);
9293
}
9394

95+
public static bool IsDbConnectionHealthy(string queryText)
96+
{
97+
try
98+
{
99+
// Try to get the default database config and check if it can connect
100+
var config = SqlDbConfigsStatic.DefaultDatabase;
101+
InfoForDbConnection info = config.GetInfoForDbConnection(config.GetDefaultConnString());
102+
using (var cmd = new PxSqlCommandForTempTables(info.DataBaseType, info.DataProvider, info.ConnectionString))
103+
{
104+
cmd.ExecuteSelect(queryText);
105+
}
106+
return true;
107+
}
108+
catch (Exception)
109+
{
110+
return false;
111+
}
112+
}
113+
94114

95115
private static string ValidateLangCodeString(string input)
96116
{

0 commit comments

Comments
 (0)