MSDN 上的所有示例都将发送和接收语句放在事务中。以下脚本来自http://msdn.microsoft.com/en-US/library/bb839499(v=sql.100).aspx
问题1:为什么所有的例子都没有begin try...end try begin catch...end catch
用来处理异常?
问题2:如果消息消费/处理需要很长时间怎么办?可以对 SSB 语句进行长期事务吗?最好的方法是什么?
问题 3:如果消息名称不是,则以下代码不会结束对话'//AWDB/1DBSample/RequestMessage'
。它是一个错误吗?
DECLARE @RecvReqDlgHandle UNIQUEIDENTIFIER;
DECLARE @RecvReqMsg NVARCHAR(100);
DECLARE @RecvReqMsgName sysname;
BEGIN TRANSACTION;
WAITFOR
( RECEIVE TOP(1)
@RecvReqDlgHandle = conversation_handle,
@RecvReqMsg = message_body,
@RecvReqMsgName = message_type_name
FROM TargetQueue1DB
), TIMEOUT 1000;
-- Process.... May take a long time
IF @RecvReqMsgName =
N'//AWDB/1DBSample/RequestMessage'
BEGIN
DECLARE @ReplyMsg NVARCHAR(100);
SELECT @ReplyMsg =
N'<ReplyMsg>Message for Initiator service.</ReplyMsg>';
SEND ON CONVERSATION @RecvReqDlgHandle
MESSAGE TYPE
[//AWDB/1DBSample/ReplyMessage]
(@ReplyMsg);
END CONVERSATION @RecvReqDlgHandle;
END
SELECT @ReplyMsg AS SentReplyMsg;
COMMIT TRANSACTION;