Npgsql.NpgsqlException (0x80004005): Exception while reading from stream ---> System.TimeoutException

winanjaya 146 Reputation points
2023-10-28T13:30:42.5866667+00:00

Hi All,

Why I occasionally get:

Npgsql.NpgsqlException (0x80004005): Exception while reading from stream ---> System.TimeoutException: Timeout during reading attempt at Npgsql.Internal.NpgsqlConnector.g__ReadMessageLong|233_0(NpgsqlConnector connector, Boolean async, DataRowLoadingMode dataRowLoadingMode, Boolean readingNotifications, Boolean isReadingPrependedMessage) at Npgsql.NpgsqlDataReader.NextResult(Boolean async, Boolean isConsuming, CancellationToken cancellationToken) at Npgsql.NpgsqlDataReader.NextResult(Boolean async, Boolean isConsuming, CancellationToken cancellationToken) at Npgsql.NpgsqlCommand.ExecuteReader(CommandBehavior behavior, Boolean async, CancellationToken cancellationToken) at Npgsql.NpgsqlCommand.ExecuteReader(CommandBehavior behavior, Boolean async, CancellationToken cancellationToken) at Npgsql.NpgsqlCommand.ExecuteNonQuery(Boolean async, CancellationToken cancellationToken) at Dapper.SqlMapper.ExecuteImplAsync(IDbConnection cnn, CommandDefinition command, Object param)

CentOS 7

NetCore 6

PostgreSQL 14

What did I do wrong?

Server=pg.example.com;Database=mydb;Port=5432;User ID=example;Password=example!!;Keepalive=300; Pooling=true;Minimum Pool Size = 0; Maximum Pool Size = 500; Connection Idle Lifetime=300; CommandTimeout=300; KeepAlive=300; Tcp Keepalive=true; Include Error Detail=true

{

try
{
    using (NpgsqlConnection conn = new NpgsqlConnection(Models.AppSettings.PG_SQL.Connection_String))
    {
        try
        {
            string sql = "Delete From cdrs Where EXTRACT(EPOCH FROM (Current_Timestamp at time zone 'UTC' - cdatetime at time zone 'UTC')) / @Seconds_To_Year > @Max_Data_Retention;" +
                "Delete From sessions Where EXTRACT(EPOCH FROM (Current_Timestamp at time zone 'UTC' - cdatetime at time zone 'UTC')) / @Seconds_To_Year > @Max_Data_Retention";

            int numDeleted = await conn.ExecuteAsync(sql, new
            {
                Seconds_To_Year = 31536000,
                Max_Data_Retention = Models.AppSettings.Max_Data_Retention
            });

            if (numDeleted > 0)
            {
                await Models.Audit.AddAsync((long)0, numDeleted.ToString() + " Old CDRs & Sessions Data Removed");
            }
        }
        catch (Exception e) { Helper.SaveLogAsync("CDR.Remove_Old_DataAsync: " + e.ToString(), Models.Errs.ErrType.Err); }
        finally { }
    }
}
catch (Exception e) { Helper.SaveLogAsync("CDR.Remove_Old_DataAsync: " + e.ToString(), Models.Errs.ErrType.Err); }
finally { }
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,573 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Olaf Helper 44,816 Reputation points
    2023-10-28T16:10:31.26+00:00

    Npgsql nor PostGreSql is a Microsoft product, so better post to a more related forum

    https://www.npgsql.org/

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.