ADO.NET 如何实现SqlCommand.CommandTimeout 工作吗?
考虑一个存储过程,它在不使用事务的情况下大约在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
答案是否定的,您在服务器上尝试的操作将在 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...
我想知道完全相同的事情。我找不到答案,但做了一些实验,看起来像是发送了某种取消查询:
我知道这是一篇五年前的帖子,但如果我到达这里,其他人也会:-)
顺便说一句,在 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:
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.
如果您尚未关闭 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.)