步骤1:创建服务以接收通知和队列:
use msdb;
go
create queue dbm_notifications_queue;
create service dbm_notification_service
on queue dbm_notifications_queue
([http://schemas.microsoft.com/SQL/Notifications/PostEventNotification]);
go
create event notification dbm_notifications
on server
for database_mirroring_state_change
to service N'dbm_notification_service', N'current database';
go
请注意,我正在使用msdb
,这不是偶然的。由于服务器级事件通知是从服务器发送的,msdb
因此如果您还在中创建相反的会话端点(目标),则更好msdb
,这意味着还必须在中部署目标服务和队列msdb
。
步骤2:创建事件通知处理过程:
use msdb;
go
create table dbm_notifications_errors (
incident_time datetime not null,
session_id int not null,
has_rolled_back bit not null,
[error_number] int not null,
[error_message] nvarchar(4000) not null,
[message_body] varbinary(max));
create clustered index cdx_dbm_notifications_errors
on dbm_notifications_errors (incident_time);
go
create table mirroring_alerts (
alert_time datetime not null,
start_time datetime not null,
processing_time datetime not null,
database_id smallint not null,
database_name sysname not null,
[state] tinyint not null,
[text_data] nvarchar(max),
event_data xml not null);
create clustered index cdx_mirroring_alerts
on mirroring_alerts (alert_time);
go
create procedure dbm_notifications_procedure
as
begin
declare @dh uniqueidentifier, @mt sysname, @raw_body varbinary(max), @xml_body xml;
begin transaction;
begin try;
receive top(1)
@dh = conversation_handle,
@mt = message_type_name,
@raw_body = message_body
from dbm_notifications_queue;
if N'http://schemas.microsoft.com/SQL/Notifications/EventNotification' = @mt
begin
set @xml_body = cast(@raw_body as xml);
-- shred the XML and process it accordingly
-- IMPORTANT! IMPORTANT!
-- DO NOT LOOK AT sys.database_mirroring
-- The view represents the **CURRENT** state
-- This message reffers to an **EVENT** that had occured
-- the current state may or may no be relevant for this **PAST** event
declare @alert_time datetime
, @start_time datetime
, @processing_time datetime = getutcdate()
, @database_id smallint
, @database_name sysname
, @state tinyint
, @text_data nvarchar(max);
set @alert_time = @xml_body.value (N'(//EVENT_INSTANCE/PostTime)[1]', 'DATETIME');
set @start_time = @xml_body.value (N'(//EVENT_INSTANCE/StartTime)[1]', 'DATETIME');
set @database_id = @xml_body.value (N'(//EVENT_INSTANCE/DatabaseID)[1]', 'SMALLINT');
set @database_name = @xml_body.value (N'(//EVENT_INSTANCE/DatabaseName)[1]', 'SYSNAME');
set @state = @xml_body.value (N'(//EVENT_INSTANCE/State)[1]', 'TINYINT');
set @text_data = @xml_body.value (N'(//EVENT_INSTANCE/TextData)[1]', 'NVARCHAR(MAX)');
insert into mirroring_alerts (
alert_time,
start_time,
processing_time,
database_id,
database_name,
[state],
text_data,
event_data)
values (
@alert_time,
@start_time,
@processing_time,
@database_id,
@database_name,
@state,
@text_data,
@xml_body);
end
else if N'http://schemas.microsoft.com/SQL/ServiceBroker/Error' = @mt
begin
set @xml_body = cast(@raw_body as xml);
DECLARE @error INT
, @description NVARCHAR(4000);
WITH XMLNAMESPACES ('http://schemas.microsoft.com/SQL/ServiceBroker/Error' AS ssb)
SELECT @error = CAST(@xml_body AS XML).value('(//ssb:Error/ssb:Code)[1]', 'INT'),
@description = CAST(@xml_body AS XML).value('(//ssb:Error/ssb:Description)[1]', 'NVARCHAR(4000)');
insert into dbm_notifications_errors(
incident_time,
session_id,
has_rolled_back,
[error_number],
[error_message],
[message_body])
values (
getutcdate(),
@@spid,
0,
@error,
@description,
@raw_body);
end conversation @dh;
end
else if N'http://schemas.microsoft.com/SQL/ServiceBroker/EndDialog' = @mt
begin
end conversation @dh;
end
commit;
end try
begin catch
declare @xact_state int = xact_state(),
@error_number int = error_number(),
@error_message nvarchar(4000) = error_message(),
@has_rolled_back bit = 0;
if @xact_state = -1
begin
-- Doomed transaction, it must rollback
rollback;
set @has_rolled_back = 1;
end
else if @xact_state = 0
begin
-- transaction was already rolled back (deadlock?)
set @has_rolled_back = 1;
end
insert into dbm_notifications_errors(
incident_time,
session_id,
has_rolled_back,
[error_number],
[error_message],
[message_body])
values (
getutcdate(),
@@spid,
@has_rolled_back,
@error_number,
@error_message,
@raw_body);
if (@has_rolled_back = 0)
begin
commit;
end
end catch
end
go
编写服务代理程序不是您的常规代码。一个人必须遵循某些标准,并且很容易流入流沙地区。此代码显示了一些好的做法:
- 将消息出队和处理包装在事务中。没脑子,很明显。
- 始终检查收到的消息类型。良好的服务代理程序必须处理
Error
,并EndDialog
通过结束从它的侧面对话框适当的消息。不这样做会导致手柄泄漏(sys.conversation_endpoints
增长)
- 始终检查邮件是否已被RECEIVE出队。一些样本在之后检查@@ rowcount
RECEIVE
,这完全可以。此示例代码依赖于消息名称检查(没有消息表示NULL消息类型名称),并隐式处理该情况。
- 创建一个处理错误表。如果消息只是消失而没有跟踪,则SSB激活过程的背景性质使得很难对错误进行故障排除。
此外,此代码还针对手头的任务(监视DBM)做了一些良好的实践代码:
- 区分
post_time
(通知何时发送?),start_time
(触发通知的操作何时开始?)和processing_time
(通知何时处理?)。post_time
并且start_time
将可能是相同或非常接近,但processing_time
可以是秒,小时,天除了post_time
。有趣的审计通常是post_time
。
- 因为
post_time
和processing_time
是不同的,它应该是显而易见的是一个DBM在偶数通知监测任务激活程序没有业务看着sys.database_mirroring
视图。该视图将显示处理时的当前状态,该状态可能与事件无关或无关。如果在事件发布后很长时间进行处理(认为是维护停机时间),显然不是问题所在,但是如果DBM的状态变化非常快,并且可以在事件中发布两个(或多个)事件,那么它也可以处理“健康”的处理。行(经常发生):在这种情况下,处理过程(如您发布的代码中所述)在事件发生时进行审核,但将记录当前的final状态。阅读此类审核可能会在以后造成很大的混乱。
- 始终审核原始XML事件。这样,您以后便可以在此XML中查询未“切碎”到审计表中各列的任何信息。
步骤3:将过程附加到队列:
alter queue dbm_notifications_queue
with activation (
status=on,
procedure_name = [dbm_notifications_procedure],
max_queue_readers = 1,
execute as owner);