Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Npgsql strange exception

This is an example given in the manual. When I try the same, i get umpteen exceptions. What is the mistake in this.

using(NpgsqlConnection conn = new NpgsqlConnection("connstring"))
{
    conn.Open();
    using(NpgsqlCommand command = new NpgsqlCommand("select command", conn))
    {
        command.Parameters.Add(new NpgsqlParameter("column1", NpgsqlDbType.Integer);
        command.Parameters[0].Value = 4;
        using(NpgsqlDataReader dr = command.ExecuteReader())
        {
            dr.Read();
            Console.Write("{0} \t", dr[0]);
        }
    }
}

Exception :

System exception System.IO.IOException: I/O error occurred.
at Npgsql.NpgsqlState.<ProcessBackendResponses_Ver_3>d__a.MoveNext()
at Npgsql.NpgsqlState.IterateThroughAllResponses(IEnumerable`1 ienum)
at Npgsql.NpgsqlState.Query(NpgsqlConnector context, NpgsqlCommand command)
at Npgsql.NpgsqlConnector.Query(NpgsqlCommand queryCommand)
at Npgsql.NpgsqlConnector.ReleaseRegisteredListen()
at Npgsql.NpgsqlConnector.ReleaseResources()
at Npgsql.NpgsqlConnectorPool.UngetPooledConnector(NpgsqlConnection Connection, NpgsqlConnector Connector)
at Npgsql.NpgsqlConnectorPool.ReleasePooledConnectorInternal(NpgsqlConnection Connection, NpgsqlConnector Connector)
at Npgsql.NpgsqlConnectorPool.ReleasePooledConnector(NpgsqlConnection Connection, NpgsqlConnector Connector)
at Npgsql.NpgsqlConnectorPool.ReleaseConnector(NpgsqlConnection Connection, NpgsqlConnector Connector)
at Npgsql.NpgsqlConnection.Close()
at Npgsql.NpgsqlConnection.Dispose(Boolean disposing)
at System.ComponentModel.Component.Dispose()

Now this works correctly. What's the difference :

using(NpgsqlConnection conn = new NpgsqlConnection("connstring"))
{
    conn.Open();
    using(NpgsqlCommand command = new NpgsqlCommand("select command", conn))
    {
        command.Parameters.Add(new NpgsqlParameter("column1", NpgsqlDbType.Integer);
        command.Parameters[0].Value = 4;
        NpgsqlDataReader dr = command.ExecuteReader();            
        dr.Read();
        Console.Write("{0} \t", dr[0]);
    }
}

why datareader cannot be used with Idisposable ?

like image 611
King Avatar asked Dec 01 '25 17:12

King


1 Answers

Npgsql doesn't handle connection interuptions if you are using datareader with the Idisposable. If the server is terminating the connection before it is communicating to the application and when the connection returns to the connection pool , it goes to the server to check the connection and by this time, the datareader doesn't get disposed properly. In a multi-threaded environment, this will come up in unfortunate circumstances. If the server is disconnecting itself from an connection, it should not terminate so immediately that the Npgsql is not able to communicate back to it. Increase the server's disconnecting time to be higher. Mine is a case of disconnecting the server abruptly forcibly.

Solution : Npgsql should handle the exception internally which is not yet present.

like image 77
2 revsKing Avatar answered Dec 03 '25 06:12

2 revsKing



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!