我决定将日志记录添加到SQL Broker Queue的Activation存储过程 - 目标是记录错误。经过测试,日志表不断充满错误。
在添加记录之前:
添加记录后
服务器性能仍然只显示“blip”,但是......
服务队列“OutboundEventRequestQueue”目前已被禁用。
在运行以下命令
之前,日志记录不会停止ALTER DATABASE [MyDatabaseName] SET NEW_BROKER with ROLLBACK IMMEDIATE
存储程序:
ALTER PROCEDURE [dbo].[usp_OutboundEventRequestQueueActivation]
AS
BEGIN
SET NOCOUNT ON;
DECLARE @ConversationHandle UNIQUEIDENTIFIER;
DECLARE @MessageBody XML;
DECLARE @MessageTypeName SYSNAME;
---------------
-- READS ALL THE MESSAGES IN THE QUEUE
---------------
WHILE (1=1)
BEGIN
BEGIN TRY
BEGIN TRANSACTION;
---------------
-- KEEPS READING While In 'Notified' State
---------------
WAITFOR
(
RECEIVE TOP (1)
@ConversationHandle = conversation_handle,
@MessageBody = CAST(message_body AS XML),
@MessageTypeName = message_type_name
FROM [dbo].[OutboundEventRequestQueue]
), TIMEOUT 5000;
IF (@@ROWCOUNT = 0)
BEGIN
ROLLBACK TRANSACTION;
BREAK;
END
---------------
-- HANDLE REPLY MESSAGE: OutboundEventRequest
---------------
IF @MessageTypeName = N'OutboundEventRequestMessageType'
BEGIN
-- DEBUG ONLY: Use to force an error
--WAITFOR DELAY '00:00:01'
--------------
-- INSERT AUDIT
--------------
DECLARE @AuditedEvent XML;
EXEC [event].[usp_tOutbound_Audit_UPSERT] @MessageBody, @AuditedEvent = @AuditedEvent OUTPUT
---------------
-- DO WORK
---------------
-- Publish XML Message (payload) to Outbound Event Aggregator (using SQL CLR)
DECLARE @Payload NVARCHAR(MAX) = (SELECT CONVERT(NVARCHAR(MAX), @AuditedEvent))
EXEC dbo.usp_OutboundEventMessage_PUBLISH @outboundEvent = @Payload
-- SEND REPLY
DECLARE @ReplyMessageBody XML = @MessageBody;
SEND ON CONVERSATION @ConversationHandle MESSAGE TYPE [OutboundEventResponseMessageType] (@ReplyMessageBody);
END
---------------
-- HANDLE ERRORS: for EndDialog MessageTypes
---------------
ELSE IF @MessageTypeName = N'http://schemas.microsoft.com/SQL/ServiceBroker/EndDialog'
BEGIN
-- CLOSE CONVERSATION
END CONVERSATION @ConversationHandle;
END
---------------
-- HANDLE ERRORS: for Error MessageTypes
---------------
ELSE IF @MessageTypeName = N'http://schemas.microsoft.com/SQL/ServiceBroker/Error'
BEGIN
-- CLOSE CONVERSATION
END CONVERSATION @ConversationHandle;
END
COMMIT TRANSACTION;
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION;
DECLARE @Error_Number INT = (SELECT ERROR_NUMBER())
DECLARE @Error_Message VARCHAR(MAX) = (SELECT ERROR_MESSAGE())
DECLARE @Error_Severity INT = (SELECT ERROR_SEVERITY())
DECLARE @Error_State INT = (SELECT ERROR_STATE())
DECLARE @Error_Procedure VARCHAR(400) = (SELECT ERROR_PROCEDURE())
DECLARE @Error_Line INT = (SELECT ERROR_LINE())
-- HERE: The log-table is continuously FILLING-UP
EXEC [event].[usp_Exceptions_TRYINSERT]
@Error_Number,
@Error_Message,
@Error_Severity,
@Error_State,
@Error_Procedure,
@Error_Line
END CATCH
END
END
THE QUEUE:
ALTER QUEUE [dbo].[OutboundEventRequestQueue] WITH STATUS = OFF , RETENTION = OFF , ACTIVATION ( STATUS = ON , PROCEDURE_NAME = [dbo].[usp_OutboundEventRequestQueueActivation] , MAX_QUEUE_READERS = 10 , EXECUTE AS OWNER ), POISON_MESSAGE_HANDLING (STATUS = ON)
答案 0 :(得分:0)
好的,我现在觉得很蠢。我很惊讶没有人抓住这个...我很惊讶我没有。
问题代码:
WHILE(1 = 1)
经过纠正的存储程序:
这将正确捕获错误
ALTER PROCEDURE [dbo].[usp_OutboundEventRequestQueueActivation]
AS
BEGIN
SET NOCOUNT ON;
DECLARE @ConversationHandle UNIQUEIDENTIFIER;
DECLARE @MessageBody XML;
DECLARE @MessageTypeName SYSNAME;
DECLARE @RunningState TINYINT = 1;
---------------
-- READS ALL THE MESSAGES IN THE QUEUE
--------------
WHILE (@RunningState = 1)
BEGIN
BEGIN TRY
BEGIN TRANSACTION;
---------------
-- KEEPS READING While In 'Notified' State
---------------
WAITFOR
(
RECEIVE TOP (1)
@ConversationHandle = conversation_handle,
@MessageBody = CAST(message_body AS XML),
@MessageTypeName = message_type_name
FROM [dbo].[OutboundEventRequestQueue]
), TIMEOUT 5000;
IF (@@ROWCOUNT = 0)
BEGIN
ROLLBACK TRANSACTION;
BREAK;
END
---------------
-- HANDLE REPLY MESSAGE: OutboundEventRequest
---------------
IF @MessageTypeName = N'OutboundEventRequestMessageType'
BEGIN
-- DEBUG ONLY: Use to force an error
--WAITFOR DELAY '00:00:01'
--------------
-- INSERT AUDIT
--------------
DECLARE @AuditedEvent XML;
EXEC [event].[usp_tOutbound_Audit_UPSERT] @MessageBody, @AuditedEvent = @AuditedEvent OUTPUT
---------------
-- DO WORK
---------------
-- Publish XML Message (payload) to Outbound Event Aggregator (using SQL CLR)
DECLARE @Payload NVARCHAR(MAX) = (SELECT CONVERT(NVARCHAR(MAX), @AuditedEvent))
EXEC dbo.usp_OutboundEventMessage_PUBLISH @outboundEvent = @Payload
-- SEND REPLY
DECLARE @ReplyMessageBody XML = @MessageBody;
SEND ON CONVERSATION @ConversationHandle MESSAGE TYPE [OutboundEventResponseMessageType] (@ReplyMessageBody);
END
---------------
-- HANDLE ERRORS: for EndDialog MessageTypes
---------------
ELSE IF @MessageTypeName = N'http://schemas.microsoft.com/SQL/ServiceBroker/EndDialog'
BEGIN
-- CLOSE CONVERSATION
END CONVERSATION @ConversationHandle;
END
---------------
-- HANDLE ERRORS: for Error MessageTypes
---------------
ELSE IF @MessageTypeName = N'http://schemas.microsoft.com/SQL/ServiceBroker/Error'
BEGIN
-- CLOSE CONVERSATION
END CONVERSATION @ConversationHandle;
END
COMMIT TRANSACTION;
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION;
SET @RunningState = 0;
DECLARE @Error_Number INT = (SELECT ERROR_NUMBER())
DECLARE @Error_Message VARCHAR(MAX) = (SELECT ERROR_MESSAGE())
DECLARE @Error_Severity INT = (SELECT ERROR_SEVERITY())
DECLARE @Error_State INT = (SELECT ERROR_STATE())
DECLARE @Error_Procedure VARCHAR(400) = (SELECT ERROR_PROCEDURE())
DECLARE @Error_Line INT = (SELECT ERROR_LINE())
EXEC [event].[usp_Exceptions_TRYINSERT]
@Error_Number,
@Error_Message,
@Error_Severity,
@Error_State,
@Error_Procedure,
@Error_Line
END CATCH
END
END