2024-02-22 16:43:59 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using Npgsql;
|
|
|
|
|
using System.Data;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
|
|
|
|
|
namespace SafeMobileLib
|
|
|
|
|
{
|
|
|
|
|
public class DBmanager
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
public NpgsqlConnection cn;
|
|
|
|
|
private string p_server;
|
|
|
|
|
private string p_dbname;
|
|
|
|
|
private string p_user;
|
|
|
|
|
private string p_password;
|
|
|
|
|
private string p_port;
|
|
|
|
|
String MyConString = "";
|
|
|
|
|
public static int countConn = 0;
|
|
|
|
|
|
|
|
|
|
public DBmanager(string p_server, string p_dbname, string p_user, string p_password, string p_port)
|
|
|
|
|
{
|
|
|
|
|
this.p_server = p_server;
|
|
|
|
|
this.p_dbname = p_dbname;
|
|
|
|
|
this.p_user = p_user;
|
|
|
|
|
this.p_password = p_password;
|
|
|
|
|
this.p_port = p_port;
|
|
|
|
|
MyConString = getConnectionString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void InitConnection()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
cn = new NpgsqlConnection();
|
|
|
|
|
cn.ConnectionString = getConnectionString();
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
cn.Open();
|
|
|
|
|
|
|
|
|
|
Interlocked.Increment(ref countConn); //countConn = countConn + 1;
|
|
|
|
|
|
|
|
|
|
Utils.WriteLine($"Open a new connection. Active connections : {countConn}", ConsoleColor.DarkGreen);
|
|
|
|
|
if (countConn > 280)
|
|
|
|
|
Utils.WriteLine($"Max limit of active connections is abount to be reached: {countConn}/300", ConsoleColor.Red);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception o)
|
|
|
|
|
{
|
|
|
|
|
if (cn != null)
|
|
|
|
|
{
|
|
|
|
|
cn.Close();
|
|
|
|
|
}
|
|
|
|
|
if (o.ToString().Contains("The connection pool has been exhausted"))
|
|
|
|
|
{
|
|
|
|
|
NpgsqlConnection.ClearAllPools();
|
|
|
|
|
countConn = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public sqlResponse RunCommand(string command)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
sqlResponse toReturn = sqlResponse.done;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using (NpgsqlConnection connection = new NpgsqlConnection())
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
connection.ConnectionString = getConnectionString();
|
|
|
|
|
connection.Open();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
using (NpgsqlCommand cmd = new NpgsqlCommand(command, connection))
|
|
|
|
|
{
|
|
|
|
|
cmd.ExecuteNonQuery();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ee)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(ee.Message + " " + ee.StackTrace + " " + ee.Source, ConsoleColor.Red);
|
|
|
|
|
toReturn = sqlResponse.SQLerror;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return toReturn;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int GetInt32Result(string command)
|
|
|
|
|
{
|
|
|
|
|
string result = GetStringResult(command);
|
|
|
|
|
if (result == string.Empty)
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
return Int32.Parse(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public string GetStringResult(string command)
|
|
|
|
|
{
|
|
|
|
|
string result = string.Empty;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using (NpgsqlConnection connection = new NpgsqlConnection())
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
connection.ConnectionString = getConnectionString();
|
|
|
|
|
connection.Open();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
using (NpgsqlCommand cmd = new NpgsqlCommand(command, connection))
|
|
|
|
|
{
|
|
|
|
|
using (NpgsqlDataReader reader = cmd.ExecuteReader())
|
|
|
|
|
{
|
|
|
|
|
reader.Read();
|
|
|
|
|
result = reader.GetValue(0).ToString();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ee)
|
|
|
|
|
{
|
|
|
|
|
if(!ee.ToString().Contains("No row is available"))
|
|
|
|
|
Console.WriteLine(ee.ToString(), ConsoleColor.Red);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int CommandTimeout = 60;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public String getConnectionString()
|
|
|
|
|
{
|
|
|
|
|
return String.Format("Server={0};Port={1};" +
|
|
|
|
|
"User Id={2};Password={3};Database={4};Pooling=true;MinPoolSize=30;MaxPoolSize=300;Timeout=15;CommandTimeout={5};",
|
|
|
|
|
p_server, p_port, p_user, p_password, p_dbname, CommandTimeout);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-05 11:00:51 +00:00
|
|
|
|
|
2024-02-22 16:43:59 +00:00
|
|
|
|
|
|
|
|
|
public void CloseConnection()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (cn != null)
|
|
|
|
|
{
|
|
|
|
|
cn.Close();
|
|
|
|
|
|
|
|
|
|
Interlocked.Decrement(ref countConn); //countConn = countConn - 1;
|
|
|
|
|
if (countConn > 280)
|
|
|
|
|
{
|
|
|
|
|
Utils.WriteLine("CLOSED : " + countConn, ConsoleColor.Blue);
|
|
|
|
|
NpgsqlConnection.ClearAllPools();
|
|
|
|
|
countConn = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
catch(Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Utils.WriteLine(ex.ToString(), ConsoleColor.Magenta);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public enum sqlResponse
|
|
|
|
|
{
|
|
|
|
|
done,
|
|
|
|
|
SQLerror,
|
|
|
|
|
noUpdate
|
|
|
|
|
}
|
|
|
|
|
}
|