使用dbms_rectifier_diff解决高级复制中的数据冲突问题
作者:eygle
日期:January 19, 2005
« Oracle基于时间点的恢复 | Blog首页 | 关于Oracle的冲突解决机制的研究 »
很多时候在高级复制中可能存在数据冲突和不一致现象。
Oracle提供的dbms_rectifier_diff包可以用于解决该冲突。
以下通过实例来说明一下该Package的用法。
1.创建复制组及复制对象
SQL> execute dbms_repcat.create_master_repgroup('rep_tt');
PL/SQL procedure successfully completed
SQL> select gname,master,status from dba_repgroup;
GNAME MASTER STATUS
------------------------------ ------ ---------
REP_TT Y QUIESCED
SQL> execute dbms_repcat.create_master_repobject(sname=>'hawa',oname=>'test', type=>'table',use_existing_object=>true,gname=>'rep_tt',copy_rows=>false);
PL/SQL procedure successfully completed
SQL>
SQL> execute dbms_repcat.generate_replication_support('hawa','test','table');
PL/SQL procedure successfully completed
SQL> select gname, master, status from dba_repgroup;
GNAME MASTER STATUS
------------------------------ ------ ---------
REP_TT Y QUIESCED
SQL> select * from dba_repobject;
SNAME ONAME TYPE STATUS GENERATION_STATUS ID OBJECT_COMMENT GNAME MIN_COMMUNICATION REPLICATION_TRIGGER_EXISTS INTERNAL_PACKAGE_EXISTS GROUP_OWNER NESTED_TABLE
------------------------------ ------------------------------ ---------------- ---------- ----------------- ---------- -------------------------------------------------------------------------------- -----------------
HAWA TEST TABLE VALID GENERATED 8620 REP_TT Y Y Y PUBLIC N
HAWA TEST$RP PACKAGE VALID 8641 SYSTEM-GENERATED: REPLICATION REP_TT PUBLIC
HAWA TEST$RP PACKAGE BODY VALID 8677 SYSTEM-GENERATED: REPLICATION REP_TT PUBLIC
3 rows selected
SQL>
SQL> execute dbms_repcat.add_master_database(gname=>'rep_tt',master=>'AUTHAA.COOLYOUNG.COM.CN',use_existing_objects=>true, copy_rows=>false, propagation_mode => 'synchronous');
PL/SQL procedure successfully completed
SQL> execute dbms_repcat.resume_master_activity('rep_tt',true);
PL/SQL procedure successfully completed
SQL> select * from dba_repgroup;
SNAME MASTER STATUS SCHEMA_COMMENT GNAME FNAME RPC_PROCESSING_DISABLED OWNER
-------- ------------------ ---- ------ ---- ---- -----------------------------------------------
REP_TT Y NORMAL REP_TT N PUBLIC
2.创建保存冲突数据的数据表
a.missing_rows表用以保存冲突行
SQL> create table hawa.missing_rows_test
2 as
3 select * from hawa.test where 1=0;
Table created
b.用于保存缺失行位置及rowid
SQL> create table hawa.MISSING_LOCATION_TEST (
2 present VARCHAR2(128),
3 absent VARCHAR2(128),
4 r_id ROWID);
Table created
3.使用dbms_rectifier_diff.DIFFERENCES查找缺失记录
SQL> begin dbms_rectifier_diff.DIFFERENCES(
2 SNAME1 =>'HAWA',
3 ONAME1 =>'TEST',
4 REFERENCE_SITE =>'AVATAR.COOLYOUNG.COM.CN',
5 SNAME2 =>'HAWA',
6 ONAME2 =>'TEST',
7 COMPARISON_SITE =>'AUTHAA.COOLYOUNG.COM.CN',
8 WHERE_CLAUSE =>NULL,
9 COLUMN_LIST =>NULL,
10 MISSING_ROWS_SNAME =>'HAWA',
11 MISSING_ROWS_ONAME1 =>'MISSING_ROWS_TEST',
12 MISSING_ROWS_ONAME2 =>'MISSING_LOCATION_TEST',
13 MISSING_ROWS_SITE =>'AVATAR.COOLYOUNG.COM.CN',
14 MAX_MISSING =>500,
15 COMMIT_ROWS =>100
16 );
17 end;
18 /
PL/SQL procedure successfully completed
冲突记录被保存在我们创建的指定表中
SQL> select count(*) from hawa.missing_rows_test;
COUNT(*)
----------
172
共有172条差异记录
SQL> select count(*) from hawa.test;
COUNT(*)
----------
548
SQL> select count(*) from hawa.test@authaa;
COUNT(*)
----------
376
SQL> select count(*) from hawa.missing_location_test;
COUNT(*)
----------
172
4.使用DBMS_RECTIFIER_DIFF.RECTIFY进行数据整合
首先需要注意的是:
RECTIFY过程使用DIFFERENCES产生的数据进行数据调整。
在第一个表中存在,在第二个表中不存在的数据将被插入第二张表。
在第二个表中存在,在第一个个表中不存在的数据将被从第二张表中删除。
另外,在这个数据纠正过程中,你可以使用dbms_repcat.suspend_master_activity将复制组暂时挂起。
这样便于保证数据完整性。
但这不是必须的,如果复制一直激活,可能会有新的冲突出现。
SQL> BEGIN DBMS_RECTIFIER_DIFF.RECTIFY(
2 SNAME1 =>'HAWA',
3 ONAME1 =>'TEST',
4 REFERENCE_SITE =>'AVATAR.COOLYOUNG.COM.CN',
5 SNAME2 =>'HAWA',
6 ONAME2 =>'TEST',
7 COMPARISON_SITE =>'AUTHAA.COOLYOUNG.COM.CN',
8 COLUMN_LIST =>NULL,
9 MISSING_ROWS_SNAME =>'HAWA',
10 MISSING_ROWS_ONAME1 =>'MISSING_ROWS_TEST',
11 MISSING_ROWS_ONAME2 =>'MISSING_LOCATION_TEST',
12 MISSING_ROWS_SITE =>'AVATAR.COOLYOUNG.COM.CN',
13 COMMIT_ROWS =>100
14 );
15 END;
16 /
PL/SQL procedure successfully completed
SQL> select count(*) from hawa.test@authaa;
COUNT(*)
----------
548
SQL> select count(*) from hawa.test;
COUNT(*)
----------
548
数据矫正完成以后,数据会自动从missing_rows表中删除。
SQL> select count(*) from hawa.missing_rows_test;
COUNT(*)
----------
0
SQL>