i have a simple code that tries to simulate a load test for my Postgres database
List<Values> values = new List<Values>();
for (int i = 0; i < 200; i++)
{
var mythread = new Thread(() =>
{
DatabaseConnection db = new DatabaseConnection();
using (var conn = db.GetDatabaseConnection())
{
var cmd = new NpgsqlCommand();
cmd.Connection = conn;
cmd.CommandText = "SELECT val.id, val.name FROM val.myvalue val";
cmd.Prepare();
var reader = cmd.ExecuteReader();
try
{
while (reader.Read())
{
Values value = new Values();
value.Id = reader.GetInt64(0);
value.Name = reader.GetString(1);
values.Add(value);
}
reader.Close();
}
catch (Exception ex)
{
reader.Close();
var exception = ex;
throw exception;
}
}
});
mythread.Start();
}
but when I run this I get the error Npgsql.PostgresException: '53300: sorry, too many clients already'
Database connection class
public class DatabaseConnection
{
public NpgsqlConnection GetDatabaseConnection()
{
var connectionstring = "User ID=myuser;Password=mypassword;Server=localhost;Port=5432;Database=myvaluesdb;Pooling=True;Minimum Pool Size=10;Maximum Pool Size=100;Trust Server Certificate=true;Connection Idle Lifetime=300;keepalive=10;Timeout=60";
NpgsqlConnection connection = null;
try
{
connection = new NpgsqlConnection(connectionstring);
connection.Open();
}
catch (Exception)
{
// try to reconnect
connection = new NpgsqlConnection(connectionstring);
connection.Open();
}
if (connection.State == ConnectionState.Open)
Console.WriteLine("connection status = OPEN");
else
{
var exception = new Exception("could not get DB connection");
throw exception;
}
return connection;
}
}
checking the max_connections shows 100
so my question is is this normal or is there something am doing wrong that needs to be corrected and if so how can i correct this
Thanks