ADO.NET 如何实现SqlCommand.CommandTimeout 工作吗?

发布于 2024-09-12 04:31:46 字数 336 浏览 1 评论 0原文

考虑一个存储过程,它在不使用事务的情况下大约在 60 秒内更新一些行。我们设置 ADO.NET 的 SqlCommand.Timeout 为 30 秒。

SqlCommand.Timeout = 30;

当超时发生在 30 秒时,存储过程是否会继续在数据库服务器中运行?服务器如何将此信息传达给客户端?

Consider a stored procedure that updates some rows about in 60 seconds without using a transaction. We set ADO.NET's SqlCommand.Timeout to 30 seconds.

SqlCommand.Timeout = 30;

When that timeout occurs at 30 seconds, will the stored procedure continue to run in database server or not? How does the server communicate this to the client?

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

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

发布评论

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

评论(3

无悔心 2024-09-19 04:31:46

答案是否定的,您在服务器上尝试的操作将在 30 秒后失败,您的 SqlCommand 对象将在代码中抛出异常(如下),并且隐式存储过程事务将回滚。

超时已过。操作完成之前超时时间已过,或者服务器没有响应。

...至少这是我可以使用 SQL Server 验证的行为...

The answer is no, your attempted action on the server will fail after 30s, your SqlCommand object will throw an exception in your code (below) and the implicit stored procedure transaction will rollback.

Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.

...at least this is the behavior that I can verify using SQL Server...

一念一轮回 2024-09-19 04:31:46

我想知道完全相同的事情。我找不到答案,但做了一些实验,看起来像是发送了某种取消查询:

  • 如果涉及触发器或表被锁定,它将不会更新。
  • 如果有多个语句,不在一个事务中,则超时后会取消这些语句。已经完成的事情仍然会完成。
  • 如果您在插入之前添加延迟,则表中不会有任何内容。
  • 如果您在插入后添加延迟,插入将被写入您的表中。

我知道这是一篇五年前的帖子,但如果我到达这里,其他人也会:-)

顺便说一句,在 ado.net 中,为了增加超时,您必须增加 sqlcommand 和 sqlconnection,第一个默认为 15 秒,第二个默认为 30 秒。因此,如果你只是将 sql 命令更改为 60 秒,它仍然会在 30 秒后超时,这可能相当令人费解。

I was wondering about the exact same. I could not find an answer, but did some experimenting, and it looks like some kind of cancel query is send:

  • If there is a trigger involved or the table is locked, it won't update.
  • if there are multiple statements, not in a transaction, it will cancel the ones after the timeout occured. What was already done stays done.
  • If you add a delay before your insert, nothing will be in your table.
  • if you add a delay after your insert, the insert will be written to your table.

I know this is a five year old post, but if I got here, others will too :-)

By the way, in ado.net, in order to increase your timeout, you must increase in both the sqlcommand and in the sqlconnection, and the defaults for the first is 15 sec, the second 30 sec. So if you just change the sqlcommand to 60 secs, it will still timeout after 30 sec, which can be rather puzzling.

傲娇萝莉攻 2024-09-19 04:31:46

如果您尚未关闭 SQL 连接,并且它已经开始执行过程,那么它应该继续运行。 (您应该能够相对轻松地测试和验证这一点。)

If you haven't closed your SQL connection yet, it should continue to run if it started proc execution already. (You should be able to test & verify this relatively easily.)

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