激活过程错误的服务中介
Service broker with activation procedure error
我使用服务代理在 sql 服务器中创建了一个目标队列来接收消息。我的目标队列是这样的:
USE [DatabaseB]
GO
ALTER QUEUE [dbo].[TargetQueue] WITH STATUS = OFF , RETENTION = OFF , ACTIVATION ( STATUS = ON , PROCEDURE_NAME = [dbo].[myp] , MAX_QUEUE_READERS = 10 , EXECUTE AS N'dbo' ), POISON_MESSAGE_HANDLING (STATUS = ON)
这是我在 target_queue
中收到消息时调用的存储过程
更改过程 [dbo].[myp]
AS
WHILE (1=1)
BEGIN
Declare @ConversationHandle as uniqueidentifier
Declare @MessageBody as nvarchar(max)
Declare @MessageType as sysname
Begin Transaction
Print 'Started Receiving ';
RECEIVE top (1)
@MessageType = message_type_name,
@ConversationHandle = conversation_handle,
@MessageBody = message_body
FROM TargetQueue;
if @MessageType = 'SenderMessageType'
Begin
SEND
ON CONVERSATION @ConversationHandle
Message Type ReceiverMessageType
('Message is received')
END Conversation @ConversationHandle
insert into table_1 (dataa) values ('salam')
END
Commit
end
但是当队列收到消息时,我的存储过程会向 table_1
添加无限数据,但我有一个条件,如您在上面看到的,我的意思是对于收到的每条消息,sp 应该添加一个行到 table 不是无限行。为什么?
我只是将存储过程更改为:
ALTER PROCEDURE [dbo].[myp]
AS
WHILE (1=1)
BEGIN
Declare @ConversationHandle as uniqueidentifier
Declare @MessageBody as nvarchar(max)
Declare @MessageType as sysname
Begin Transaction
Print 'Started Receiving ';
WAITFOR
(
RECEIVE top (1)
@MessageType = message_type_name,
@ConversationHandle = conversation_handle,
@MessageBody = message_body
FROM TargetQueue), TIMEOUT 5000;
IF (@@ROWCOUNT = 0)
BEGIN
ROLLBACK TRANSACTION;
BREAK;
END
if @MessageType = 'SenderMessageType'
Begin
SEND
ON CONVERSATION @ConversationHandle
Message Type ReceiverMessageType
('Message is received')
END Conversation @ConversationHandle
insert into table_12 (dataa) values ('salam')
END
Commit
end
我使用服务代理在 sql 服务器中创建了一个目标队列来接收消息。我的目标队列是这样的:
USE [DatabaseB]
GO
ALTER QUEUE [dbo].[TargetQueue] WITH STATUS = OFF , RETENTION = OFF , ACTIVATION ( STATUS = ON , PROCEDURE_NAME = [dbo].[myp] , MAX_QUEUE_READERS = 10 , EXECUTE AS N'dbo' ), POISON_MESSAGE_HANDLING (STATUS = ON)
这是我在 target_queue
中收到消息时调用的存储过程更改过程 [dbo].[myp]
AS
WHILE (1=1)
BEGIN
Declare @ConversationHandle as uniqueidentifier
Declare @MessageBody as nvarchar(max)
Declare @MessageType as sysname
Begin Transaction
Print 'Started Receiving ';
RECEIVE top (1)
@MessageType = message_type_name,
@ConversationHandle = conversation_handle,
@MessageBody = message_body
FROM TargetQueue;
if @MessageType = 'SenderMessageType'
Begin
SEND
ON CONVERSATION @ConversationHandle
Message Type ReceiverMessageType
('Message is received')
END Conversation @ConversationHandle
insert into table_1 (dataa) values ('salam')
END
Commit
end
但是当队列收到消息时,我的存储过程会向 table_1
添加无限数据,但我有一个条件,如您在上面看到的,我的意思是对于收到的每条消息,sp 应该添加一个行到 table 不是无限行。为什么?
我只是将存储过程更改为:
ALTER PROCEDURE [dbo].[myp]
AS
WHILE (1=1)
BEGIN
Declare @ConversationHandle as uniqueidentifier
Declare @MessageBody as nvarchar(max)
Declare @MessageType as sysname
Begin Transaction
Print 'Started Receiving ';
WAITFOR
(
RECEIVE top (1)
@MessageType = message_type_name,
@ConversationHandle = conversation_handle,
@MessageBody = message_body
FROM TargetQueue), TIMEOUT 5000;
IF (@@ROWCOUNT = 0)
BEGIN
ROLLBACK TRANSACTION;
BREAK;
END
if @MessageType = 'SenderMessageType'
Begin
SEND
ON CONVERSATION @ConversationHandle
Message Type ReceiverMessageType
('Message is received')
END Conversation @ConversationHandle
insert into table_12 (dataa) values ('salam')
END
Commit
end