存储过程和 SqlCommand 超时

发布于 2024-08-11 00:58:37 字数 88 浏览 4 评论 0原文

如果我使用 SqlCommand 运行存储过程并且 SqlCommand 超时,StoredProc 会继续执行还是在 SqlCommand 断开连接时强制退出?

If I run a stored proc using the SqlCommand and the SqlCommand times out does the StoredProc continue to execute or does it get force to quit when the SqlCommand disconnects?

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

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

发布评论

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

评论(2

最好是你 2024-08-18 00:58:37

我的直觉告诉我该程序仍然会执行,所以我进行了一个简单的测试。

SQL:

Create Procedure TestDelay
AS

waitfor delay '00:00:40'

update table_1
set dt = getdate()

在 VB.Net 中(与 C# 相同):

    Dim con As New SqlConnection(myconnectionstring)
    Dim com As New SqlCommand("TestDelay", con)
    com.CommandType = CommandType.StoredProcedure
    con.Open()
    Try
        com.ExecuteNonQuery()
    Catch ex As Exception
        con.Close()
        Response.Write(ex.Message)
    End Try

结果是什么? 该过程在超时后未完成。我检查了 SQL 探查器中的跟踪期间发生的情况,并且确实 SQL 似乎将调用包装在事务中,并且必须在超时时回滚该事务。

注意:此测试是针对 SQL 2005 运行的,但我怀疑其他版本的结果类似。

My instinct said that the procedure would still execute so I threw together a simple test.

SQL:

Create Procedure TestDelay
AS

waitfor delay '00:00:40'

update table_1
set dt = getdate()

And in VB.Net (same as C# for this purpose):

    Dim con As New SqlConnection(myconnectionstring)
    Dim com As New SqlCommand("TestDelay", con)
    com.CommandType = CommandType.StoredProcedure
    con.Open()
    Try
        com.ExecuteNonQuery()
    Catch ex As Exception
        con.Close()
        Response.Write(ex.Message)
    End Try

The result? The procedure did not complete after the timeout. I checked what was happening during a trace in SQL profiler and sure enough SQL appears to wrap the call in a transaction and must roll that transaction back on the timeout.

Note: This test was run against SQL 2005 but I'd suspect the results to be similar in other versions.

挥剑断情 2024-08-18 00:58:37

正如布兰登所说,客户端发送“中止”并且处理停止。就这么简单。

然而,它比这更复杂......

默认情况下,任何事务都不会回滚,锁只会保留在那里,直到连接关闭。如果返回到连接池并重新使用,则这算作关闭。

这就是为什么 SET XACT_ABORT ON (其他问题SO1SO2

As brendan stated, the client sends an "abort" and processing stops dead. As simple as that.

However, it's more complex than that...

Any transactions are not rolled back by default and locks are just left there until the connection is closed. If returned to the connection pool and reused, then this does not count as closing.

This is why SET XACT_ABORT ON (other questions SO1, SO2) is recommended

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