Npgsql奇怪的异常

发布于 2024-12-22 08:47:58 字数 2095 浏览 0 评论 0原文

这是手册中给出的示例。当我尝试同样的方法时,我遇到了无数的例外。这其中有什么错误呢。

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]);
        }
    }
}

例外:

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()

现在可以正常工作了。有什么区别:

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]);
    }
}

为什么 datareader 不能与 Idisposable 一起使用?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清风无影 2024-12-29 08:47:58

如果您将 datareader 与 Idisposable 一起使用,则 Npgsql 不会处理连接中断。如果服务器在与应用程序通信之前终止连接,并且当连接返回到连接池时,它会转到服务器检查连接,此时,数据读取器不会得到正确处理。在多线程环境中,这种情况会在不幸的情况下出现。如果服务器自行断开连接,则不应立即终止,以免 Npgsql 无法与其进行通信。增加服务器的断开时间更长。我的情况是突然强行断开服务器连接。

解决方案:Npgsql 应该在内部处理尚未存在的异常。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文