简体   繁体   English

SQL Server 2005 Asnyc存储过程

[英]SQL Server 2005 Asnyc stored procedure

I am currently having problems calling a stored procedure async from within a insert-update-trigger. 我目前在从insert-update-trigger内部调用存储过程异步时遇到问题。 For this I m using the service broker. 为此,我正在使用服务代理。

--message type
CREATE MESSAGE TYPE [TheMessage] VALIDATION = NONE

--contract
CREATE CONTRACT [TheContract] ([TheMessage] SENT BY ANY);

--queue
CREATE QUEUE [TheQueue] WITH ACTIVATION
(STATUS = ON, MAX_QUEUE_READERS = 1,
PROCEDURE_NAME = TheStoreProcedure,
EXECUTE AS OWNER);

--service
CREATE SERVICE [TheService] ON QUEUE [TheQueue] ([TheContract]); 

Within the trigger: 在触发器内:

DECLARE @Handle UNIQUEIDENTIFIER;

BEGIN DIALOG CONVERSATION @Handle
FROM SERVICE [TheService]
TO SERVICE 'TheService'
ON CONTRACT [TheContract]
WITH ENCRYPTION = OFF;

SEND ON CONVERSATION @Handle 
MESSAGE TYPE [TheMessage](N'some data');

Within the stored procedure: 在存储过程中:

DECLARE @Handle UNIQUEIDENTIFIER;
DECLARE @MessageType SYSNAME;

RECEIVE TOP (1)
@Handle = conversation_handle,
@MessageType = message_type_name
FROM [TheQueue];

IF(@Handle IS NOT NULL)

BEGIN

-- some statements

END

This setup doesn't seem to work. 此设置似乎无效。 The trigger does not throw any errors so I assume the message is queued. 触发器不会引发任何错误,因此我假设消息已排队。 But the receive within the stored doesn't seem to work. 但是存储中的接收似乎不起作用。 None of my statements are being executed. 我的语句均未执行。

  • Check if the message isn't retained in sys.transmission_queue . 检查消息是否未保留在sys.transmission_queue Transmission_status column should explain why the message isn't delivered. Transmission_status列应说明为什么未传递邮件。
  • Check if the message is in the queue: SELECT ... FROM [TheQueue] . 检查消息是否在队列中: SELECT ... FROM [TheQueue] If the message is there and the procedure didn't activate check the queue's is_receive_enabled status in sys.service_queues . 如果消息是存在的,程序没有激活检查队列的状态is_receive_enabled sys.service_queues If the queue is disabled, you probably rolled back 5 receives in a row during testing and triggered the poison message mechanism . 如果禁用了队列,则可能在测试期间连续回滚5个接收并触发了有毒消息机制
  • If the queue is enabled, check the queue monitors status, see Understanding Queue Monitors . 如果启用了队列,请检查队列监视器的状态,请参阅了解队列监视器
  • If the message is neither in the queue nor in transmission queue, it must been consumed by the activated procedure. 如果消息既不在队列中也不在传输队列中,则必须由已激活的过程使用。 Verify your ERRORLOG for any error output. 验证您的ERRORLOG是否有任何错误输出。 Disable activation, send a message again, then run the procedure manually from an SSMS query window see if you get any error message. 禁用激活,再次发送消息,然后从SSMS查询窗口手动运行该过程,看看是否收到任何错误消息。
  • Mae sure your activated procedure does not fall into the traps of the EXECUTE AS context. 确保您的激活过程不会落入EXECUTE AS上下文的陷阱。 See Why does feature … not work under activation? 请参阅为什么功能…在激活下不起作用? and Call a procedure in another database from an activated procedure 从激活的过程中调用另一个数据库中的过程

Ok, thanks for your answers, I fixed it. 好的,谢谢您的回答,我已将其修复。

The problem was that the service broker was disabled.. 问题是服务代理已禁用。

USE AdventureWorks
GO

ALTER DATABASE AdventureWorks SET SINGLE_USER WITH ROLLBACK IMMEDIATE
ALTER DATABASE AdventureWorks SET ENABLE_BROKER
ALTER DATABASE AdventureWorks SET MULTI_USER
GO

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

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