I have this piece of code in program.cs to initialize my pool with 5 connections in my .net6 application using postgres and Npgsql.
List<NpgsqlConnection> connections = new List<NpgsqlConnection>();
for (int i = 0; i < 5; i++)
{
NpgsqlConnection connection = new NpgsqlConnection(connectionString);
try
{
connection.Open(); // This actually opens the connection
}
catch (Exception e)
{
continue;
}
connections.Add(connection);
}
connections.ForEach(connection => { connection.Close(); });
the first attempt to creata a connection is always falis with a timeout
2024-01-11T19:54:53.7699503+09:00 40000374-0003-fc00-b63f-84710c7967bb [INF] Entity Framework Core "6.0.19" initialized '"DbContextClass"' using provider '"Npgsql.EntityFrameworkCore.PostgreSQL":"6.0.8"' with options: "CommandTimeout=15 " (d2805559)
2024-01-11T19:55:09.4702152+09:00 40000374-0003-fc00-b63f-84710c7967bb [ERR] An error occurred using the connection to database '"my_db"' on server '""'. (5fc3407c)
2024-01-11T19:55:09.5272113+09:00 40000374-0003-fc00-b63f-84710c7967bb [INF] A transient exception occurred during execution. The operation will be retried after 0ms."
""System.TimeoutException: The operation has timed out.
at Npgsql.Util.NpgsqlTimeout.Check()
at Npgsql.Util.NpgsqlTimeout.CheckAndGetTimeLeft()
at Npgsql.Util.NpgsqlTimeout.CheckAndApply(NpgsqlConnector connector)
at Npgsql.Internal.NpgsqlConnector.<Open>g__OpenCore|195_1(NpgsqlConnector conn, SslMode sslMode, NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken, Boolean isFirstAttempt)
at Npgsql.Internal.NpgsqlConnector.Open(NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken)
at Npgsql.ConnectorPool.OpenNewConnector(NpgsqlConnection conn, NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken)
at Npgsql.ConnectorPool.<Get>g__RentAsync|28_0(NpgsqlConnection conn, NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken)
at Npgsql.NpgsqlConnection.<Open>g__OpenAsync|45_0(Boolean async, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenInternalAsync(Boolean errorsExpected, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenInternalAsync(Boolean errorsExpected, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenAsync(CancellationToken cancellationToken, Boolean errorsExpected)
at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.AsyncEnumerator.InitializeReaderAsync(AsyncEnumerator enumerator, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Storage.ExecutionStrategy.<>c__DisplayClass33_0`2.<<ExecuteAsync>b__0>d.MoveNext()
--- End of stack trace from previous location ---
at Microsoft.EntityFrameworkCore.Storage.ExecutionStrategy.ExecuteImplementationAsync[TState,TResult](Func`4 operation, Func`4 verifySucceeded, TState state, CancellationToken cancellationToken)" (43386701)
System.TimeoutException: The operation has timed out.
at Npgsql.Util.NpgsqlTimeout.Check()
at Npgsql.Util.NpgsqlTimeout.CheckAndGetTimeLeft()
at Npgsql.Util.NpgsqlTimeout.CheckAndApply(NpgsqlConnector connector)
at Npgsql.Internal.NpgsqlConnector.<Open>g__OpenCore|195_1(NpgsqlConnector conn, SslMode sslMode, NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken, Boolean isFirstAttempt)
at Npgsql.Internal.NpgsqlConnector.Open(NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken)
at Npgsql.ConnectorPool.OpenNewConnector(NpgsqlConnection conn, NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken)
at Npgsql.ConnectorPool.<Get>g__RentAsync|28_0(NpgsqlConnection conn, NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken)
at Npgsql.NpgsqlConnection.<Open>g__OpenAsync|45_0(Boolean async, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenInternalAsync(Boolean errorsExpected, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenInternalAsync(Boolean errorsExpected, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenAsync(CancellationToken cancellationToken, Boolean errorsExpected)
at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.AsyncEnumerator.InitializeReaderAsync(AsyncEnumerator enumerator, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Storage.ExecutionStrategy.<>c__DisplayClass33_0`2.<<ExecuteAsync>b__0>d.MoveNext()
--- End of stack trace from previous location ---
But the following attempts to do the same is succeeded and the pool always created with 4 connections
and one more things is that hence created connections gets closed automatically after a while, if i try to call an api which requires some data from the database, the first attempt to execute the query is always fails, and it always succeeds in the second attempt
builder.Services.AddDbContext<DbContextClass>(options =>
{
options.UseNpgsql(connectionString,
options =>
{
options.CommandTimeout(15);
options.EnableRetryOnFailure(2);
}
);
});
this is my retry strategy.
any help is appreciated
have manually closedshould one of those operations fail, all the other connections will remain open. What happens if you remove that code and the hard-coded timeout inUseNpgsql? What's the actual connection string? What happens if you increase the default connection timeout?