服务代理具有激活过程错误

时间:2017-06-12 15:54:27

标签: sql-server asynchronous stored-procedures service-broker

我创建了一个目标队列,使用服务代理在sql server中接收消息。我的目标队列是这样的:

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

中收到邮件时调用的商店程序

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 ';

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应该添加一行到表不是无限的行。为什么?

1 个答案:

答案 0 :(得分:0)

我只是将商店程序更改为:

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