I am using azure postgres sql where I have one of the common method which gives me connection string,
protected readonly string databaseConnString = "Host={0}.postgres.database.azure.com;Username={1}@{2};Password={3};DB={4};Ssl Mode=Require";
protected virtual async Task<NpgsqlConnection> GetDBConnection()
{
var connString = string.Empty;
NpgsqlConnection conn;
try
{
//certificate check
conn = new NpgsqlConnection(databaseConnString);
try
{
//open the connection
await conn.OpenAsync();
}
catch (PostgresException ex)
{
}
finally
{
//close the connection
await conn.CloseAsync();
}
}
//return connection string
return conn;
}
and I am consuming above connection string everywhere in the application when again I am using open/close connection every time,
using (var conn = await GetDBConnection())
{
await conn.OpenAsync();
//do the work
await conn.CloseAsync();
}
I am using npgsql .net core library
and came to know that PgBouncer
will not help here. How can I minimize the connection open/close and can achieve connection pooling?