为什么 ODBCConnection.State 不可靠?

发布于 2024-09-13 07:06:57 字数 659 浏览 1 评论 0原文

考虑一个简单的设置:

// _conn is the OdbcConnection with a MySQL-Server (MySQL-Connector 3.51)
// _cmd is a created OdbcCommand

// Constructor has created the objects successfully

public void DoSomething() {
    if(_conn.State == ConnectionState.Open)
        _cmd.ExecuteNonQuery();
}

现在我的问题是,OdbcConnection.State 不可靠。问题是,一段时间后连接丢失,但是 State-Property 对此一无所知,并且一直告诉我连接已打开,至少直到我尝试执行为止一个命令(优雅地失败)。我什至遇到过这样的情况:状态属性永远不会刷新,并一直告诉我连接仍然存在(但命令失败)。

当然,我可以在代码中添加 Try {...} Catch {...} 块,但我试图避免它们,因为扩展了一个至少包含四行错误处理的两行函数有点……重。

所以我的问题是:为什么 OdbcConnection.State 不可靠?我可以修复它吗?

Consider a simple setup:

// _conn is the OdbcConnection with a MySQL-Server (MySQL-Connector 3.51)
// _cmd is a created OdbcCommand

// Constructor has created the objects successfully

public void DoSomething() {
    if(_conn.State == ConnectionState.Open)
        _cmd.ExecuteNonQuery();
}

Now my problem is, that OdbcConnection.State isn't reliable. The thing is that after some time the connection gets lost, but the State-Property doesn't know anything about it and keeps telling me that the connection is open at least till the point were I try to execute a command (which fails gracefully). I even had the situation were the State-Property would never refresh and kept telling me that the connection was still there (but commands failed).

Of course I could add Try {...} Catch {...} blocks to my code, but I tried to avoid them because extending a two-line function with at least four lines of error handling is a little bit...heavy.

So my question is: Why isn't OdbcConnection.State reliable and can I fix it?

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

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

发布评论

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

评论(1

请恋爱 2024-09-20 07:06:57

网络的本质是,在实际尝试发送数据之前,您通常不知道存在问题。如果您只是将插头从计算机背面拔出(或者从客户端和服务器之间的某个开关拔出插头),那么操作系统将无法知道,直到它实际尝试发送数据甚至还有问题。

那么,State 不可靠的原因是,如果不实际尝试向服务器发送数据,就不可能使其完全可靠 。由于对于一个简单的属性来说,要做的工作太多(整个网络往返),因此它不会这样做,而只是做了最简单的事情。

此外,事实上,当您调用 State == Open 和实际执行命令之间存在竞争条件:您可能调用 State == Open,然后有人拉取在执行命令之前先拔掉插头。

因此,最终,您无论如何都必须拥有该异常处理程序。我还建议您不要在每次调用数据库时都放置异常。如果您正在做一个网站,那么有一个页面级处理程序并保留它。除了向用户显示错误消息之外,尝试以任何其他方式“处理”数据库故障确实没有意义......

The nature of networking is that you often don't know there's a problem until you actually try to send data. If you simply pull the plug out of the back of the computer (or you pull the plug out of a switch somewhere in between your client and the server) then there's no way for the operating system to know until it actually tries to send data that there's even a problem.

The reason, then, that State is not reliable is because it's impossible to make it completely reliable without actually actually trying to send data to the server. Since that's far too much work for a simple property to do (a whole network roundtrip) it doesn't do that, and just does the simplest thing possible.

Also, there's the fact that there's a race condition between when you call State == Open and actually executing the command: you might call State == Open, and then someone pulls the plug before you execute the command.

So at the end of the day, you're going to have to have that exception handler anyway. I would also suggest that you don't put the exception around every single call to the database. If you're doing a website, then have a page-level handler and leave it at that. There's really no point trying "handle" a database going down in any other way than by displaying an error message to the user anyway...

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