Click here to view and discuss this page in DocCommentXchange. In the future, you will be sent there automatically.

SQL Anywhere 12.0.1 » SQL Remote » 管理 SQL Remote 系统 » 复制错误的报告和处理 » 运行错误处理过程

 

接收关于远程数据库错误的电子邮件通知

当远程数据库上发生错误时,利用以下过程发送电子邮件通知。可以使用电子邮件或寻呼系统来接收通知。

 ♦ 设置 SQL Remote 以发送错误 (SQL) 的电子邮件通知
  1. 使用 [SQL Anywhere 12] 插件以用户 Cons 的身份连接到统一数据库。

  2. 创建一个可以通过电子邮件通知 DBA 用户有错误发生的存储过程。

    例如,执行以下语句来创建 sp_LogReplicationError 过程:



    CREATE PROCEDURE cons.sp_LogReplicationError
     ( IN error_text LONG VARCHAR )
    BEGIN
     DECLARE current_remote_user CHAR( 255 );
     SET current_remote_user = CURRENT REMOTE USER;
     // Log the error
     INSERT INTO cons.replication_audit
      ( remoteuser, errormsg )
     VALUES
      ( current_remote_user, error_text );
     COMMIT WORK;
     //Now notify the DBA by email that an error has occurred
     // on the consolidated database. The email should contain the error
     // strings that the SQL Remote Message Agent is passing to the procedure. 
     IF CURRENT PUBLISHER = 'cons' THEN
      CALL sp_notify_DBA( error_text );
     END IF
    END;
  3. 创建一个管理电子邮件发送的存储过程。

    例如,执行以下语句来创建 sp_notifiy_DBA 过程:



    CREATE PROCEDURE sp_notify_DBA( in msg long varchar)
    BEGIN
     DECLARE rc INTEGER;
     rc=call xp_startmail( mail_user='davidf' );
     //If successful logon to mail
     IF rc=0 THEN
     rc=call xp_sendmail(
       recipient='Doe, John; Smith, Elton',
       subject='SQL Remote Error',
       "message"=msg);
     //If mail sent successfully, stop
      IF rc=0 THEN
       call xp_stopmail()
      END IF
     END IF
    END;
  4. 设置 replication_error 数据库选项,以调用通过电子邮件通知 DBA 有错误发生的过程。

    例如,执行以下语句以在发生错误时调用 sp_LogReplicationError 过程:

    SET OPTION PUBLIC.replication_error =
     'cons.sp_LogReplicationError';
  5. 创建一个审计表。

    例如,执行以下语句来创建 replication_audit 表:

    CREATE TABLE replication_audit (
     id   INTEGER DEFAULT AUTOINCREMENT,
     pub  CHAR(30) DEFAULT CURRENT PUBLISHER,
     remoteuser CHAR(30),
     errormsg LONG VARCHAR,
     timestamp  DATETIME DEFAULT CURRENT TIMESTAMP,
     PRIMARY KEY (id,pub)
    );

    下表对 replication_audit 表的列进行了说明:

    说明
    pub 数据库的当前发布者(标识已插入了发布者的数据库)。
    remoteuser 应用消息的远程用户(标识远程用户所属的数据库)。
    errormsg 传递给 replication_error 过程的错误消息。
  6. 测试您的过程。

    例如,在统一数据库中插入的行与远程数据库中的行使用相同的主键。将统一数据库中的这一行复制到远程数据库时,将发生主键冲突错误,并且:

    • 远程数据库的 SQL Remote 消息代理 (dbremote) 会向其输出日志输出以下消息:

      从 "cons" (0-0000000000-0) 接收到消息,SQL 语句失败:表 'reptable' 的 (-193) 主键不是唯一的 INSERT INTO cons.reptable( id,text,last_contact ) VALUES (2,'dave','1997/apr/21 16:02:38.325') COMMIT WORK
    • 以下 INSERT 语句将被发送到统一数据库:



      INSERT INTO cons.replication_audit
       ( id,
         pub,
         remoteuser,
         errormsg,
         "timestamp")
      VALUES
       ( 1,
        'cons',
        'sales',
        'primary key for table ''reptable'' is not unique (-193)',
        '1997/apr/21 16:03:13.836');
      COMMIT WORK;
    • 带有以下消息的电子邮件将被发送给 John Doe 和 Elton Smith:

      primary key for table 'reptable' is not unique (-193) 
      INSERT INTO cons.reptable( id,text,last_contact )
                  VALUES (2,'dave','1997/apr/21 16:02:52.605')
 另请参见