简体   繁体   English

处理存储过程中的超时

[英]Handling Timeouts inside stored procedures

I have a stored procedure that executes a couple of queries. 我有一个存储过程,可以执行几个查询。 Each query might fail because of a timeout. 每个查询都可能由于超时而失败。

I still want to continue to execute the other queries. 我仍然想继续执行其他查询。 Is this possible? 这可能吗? Does a try catch work for timeouts? 尝试捕获超时是否有效?

I can offcourse have different sps for each query, but that would make the application more complex. 我当然可以为每个查询使用不同的sps,但这会使应用程序更加复杂。

How you run the SQL? 您如何运行SQL?

SQL itself has no timeout, so what you describe as a problem is not possible. SQL本身没有超时,因此无法描述为问题。

The timeout is always handled on the connection level / connecting application. 超时总是在连接级别/连接应用程序上处理。 SQL Server is happy having SQL calls that last for hours or days. SQL Server很高兴能够持续数小时或数天的SQL调用。

So, unless you do sometihing "funny" / unusual the queries within the SP will not time out - the connection that calls the procedure will time out and thus rollback the transaction. 因此,除非您进行“有趣” /不寻常的操作,否则SP中的查询将不会超时-调用该过程的连接将超时并因此回滚事务。

存储过程中的查询不会超时-正在执行sproc的sql批处理会超时-因此,不能,您无法“捕获”然后处理超时。

If you are frequently getting timeouts, the fix is to performance tune the queries not to try to skip the timeouts and move onto the next query. 如果您经常遇到超时问题,则解决方法是对查询进行性能调整,以免跳过超时并转到下一个查询。

Timeouts are usually a sign of bad query design (Number 1 cause), bad database design (number 2 cause) or inadequate equipment. 超时通常表示查询设计不正确(1号原因),数据库设计不正确(2号原因)或设备不足。 All three are fixable. 这三个都是可修复的。 So fix them. 所以修复它们。

Short answer: no. 简短的回答:不。 Timeout is just like when you cancel your query in SSMS: CATCH won't catch it. 超时就像您在SSMS中取消查询时一样:CATCH不会捕获它。 I wrote up a canned answer: Your TRY block may fail, and your CATCH block may be bypassed 我写了一个固定的答案: 您的TRY块可能会失败,并且您的CATCH块可能会被绕过

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM