为什么 Service Broker 接收时间会比指定的超时时间长?

发布于 2024-12-10 10:29:18 字数 283 浏览 0 评论 0原文

我正在编写一个使用 SQL Server Service Broker 的高负载应用程序。即使我停止了应用程序,在 Management Studio 中运行以下脚本也需要 1 分 6 秒。是什么导致需要这么长时间?我以为超时会让它在半秒后停止?

WAITFOR (RECEIVE TOP(1) * FROM [targetqueuename]), TIMEOUT 500;
SELECT @@ERROR;

@@ERROR 返回 0。第一次运行花了这么长时间后,后续运行将立即返回。

I'm writing a high load application that uses SQL Server Service Broker. I have got to a state where running the following script in Management Studio takes 1 minute 6 seconds, even after I have stopped the application. What could be causing it to take so long? I thought the TIMEOUT would make it stop after half a second?

WAITFOR (RECEIVE TOP(1) * FROM [targetqueuename]), TIMEOUT 500;
SELECT @@ERROR;

@@ERROR is returning 0. After the first run taking this long, subsiquent runs are returning instantly.

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

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

发布评论

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

评论(1

絕版丫頭 2024-12-17 10:29:18

WAITFOR(RECEIVE), TIMEOUT 的工作原理是至少实际运行一次 RECEIVE。如果结果集为空,则继续等待。每当它相信它可以成功时(它在内部收到有更多消息可用的通知),它就会再次运行 RECEIVE。循环重复,直到返回行或超时。

但超时不会中断已在该循环内执行的 RECEIVE。如果 RECEIVE 需要很长时间才能在队列中查找消息(可能会发生在大型队列或 RECEIVE 执行计划错误的情况下),则无法遵守超时。请注意,即使 RECEIVE 没有找到任何消息,也可能出现这种情况,因为队列可能包含大量全部锁定的消息(更准确地说,所有消息都属于锁定的会话组)。在这种情况下,RECEIVE 可能需要很长时间才能执行,搜索未锁定的会话组,但最终仍然空手而归。

WAITFOR(RECEIVE), TIMEOUT works by actually running the RECEIVE at least once. If the result set is empty, it continues to wait. Every time it believes that it can succeed (it gets notified internally that more messages are available) it runs the RECEIVE again. Repeat in a loop until either it returns rows or it times out.

But the timeout does not interrupt a RECEIVE already executing inside this loop. If the RECEIVE is taking long to find messages in the queue (can happen with large queues or with bad execution plans for RECEIVE) then the timeout cannot be honored. Note that this can be the case even if the RECEIVE does not find any message, since the queue may contain a large number of messages all locked (more precisely all belonging to locked conversation groups). In this case the RECEIVE may take a long time to execute, searching for unlocked conversation groups and in the end still come empty handed.

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