假设我使用C#来运行长时间运行的sql Server存储过程(可以说30分钟).进一步假设我在C#中的查询中放置了1小时的超时时间,这样如果无论什么原因,SP需要比预期的更长的时间,我不会最终垄断DB.最后,假设这个存储过程有一个try / catch块来捕获错误,并做一些清理,如果它内部的任何步骤失败.
一些代码(C#):
using (sqlCommand comm = new sqlCommand("longrunningstoredproc")) { comm.Connection = conn; comm.CommandType = CommandType.StoredProcedure; comm.CommandTimeout = 3600; comm.ExecuteNonQuery(); } /* Note: no transaction is used here,the transactions are inside the stored proc itself. */
T-sql(基本相当于以下):
BEGIN TRY -- initiailize by inserting some rows into a working table somewhere BEGIN TRANS -- do long running work COMMIT TRANS BEGIN TRANS -- do long running work COMMIT TRANS BEGIN TRANS -- do long running work COMMIT TRANS BEGIN TRANS -- do long running work COMMIT TRANS BEGIN TRANS -- do long running work COMMIT TRANS -- etc. -- remove the rows from the working table (and set another data point to success) END TRY BEGIN CATCH -- remove the rows from the working table (but don't set the other data point to success) END CATCH
我的问题是,当命令从C#端超时时,sql Server会对查询做什么?它会调用SP的catch块,还是将其完全切断,以便我需要执行C#代码的清理?