stream是 oracle 11g  支持的数据同步技术, 虽然该技术已经不是什么新技术, 但目前国内采用该技术开发的软件不多见. stream 同步软件项目参与近一年, 近期软件上线实施, 效果不是很理想.  同步过程中会偶尔出现  ORA-00001, ORA-26786, ORA-26787 等常见错误.  经过几天的的研究, 开发了一个守护平台,采用 java 平台和存储过程相结合的方式自动处理以上错误. 我写的这些算法具有通用性, 不需要手工指定删除表的列,通过实践具有较好的效果. 相对于set_update_conflict_handler这个方法,简单易用. java 平台代码便不开源了, 其实核心的思想还是在存储过程中, 特分享给大家, 共同进步.

自定义 type

create or replace type myvarray_list as varray(300) of varchar2(50)
CREATE OR REPLACE PROCEDURE EXECUTE_TRANSACTION_1(applyname IN VARCHAR2,ltxnid IN VARCHAR2) IS
i NUMBER;
x NUMBER;
loopdog NUMBER;
txnid VARCHAR2(30);
source VARCHAR2(128);
msgno NUMBER;
msgcnt NUMBER;
errno NUMBER;
errmsg VARCHAR2(2000);
lcr ANYDATA;
rowlcr SYS.LCR$_ROW_RECORD;
typenm VARCHAR2(61);
res NUMBER;
command VARCHAR2(10);
old_values SYS.LCR$_ROW_LIST;
new_values SYS.LCR$_ROW_LIST;
v_code NUMBER;
v_errm VARCHAR2(1024);
object_owner VARCHAR2(30);
object_name VARCHAR2(40);
key_column myvarray_list;
remove_column myvarray_list;
remove_flag NUMBER;
remove_count NUMBER;
BEGIN
SELECT LOCAL_TRANSACTION_ID,
SOURCE_DATABASE,
MESSAGE_NUMBER,
MESSAGE_COUNT,
ERROR_NUMBER,
ERROR_MESSAGE
INTO txnid, source, msgno, msgcnt, errno, errmsg
FROM DBA_APPLY_ERROR
WHERE LOCAL_TRANSACTION_ID = ltxnid;
DBMS_OUTPUT.PUT_LINE(' --- Local Transaction ID: ' || txnid);
DBMS_OUTPUT.PUT_LINE(' --- Source Database: ' || source);
DBMS_OUTPUT.PUT_LINE(' ---Error in Message: '|| msgno);
DBMS_OUTPUT.PUT_LINE(' ---Error Number: '||errno);
DBMS_OUTPUT.PUT_LINE(' ---Message Text: '||errmsg);
i := msgno;
loopdog :=0;
WHILE i <= msgcnt LOOP
loopdog :=loopdog+1;
DBMS_OUTPUT.PUT_LINE('---message: ' || i);
lcr := DBMS_APPLY_ADM.GET_ERROR_MESSAGE(i, txnid); -- gets the LCR
typenm := lcr.GETTYPENAME();
DBMS_OUTPUT.PUT_LINE('type name: ' || typenm);
IF (typenm = 'SYS.LCR$_ROW_RECORD') THEN
res := lcr.GETOBJECT(rowlcr);
command := rowlcr.GET_COMMAND_TYPE();
DBMS_OUTPUT.PUT_LINE('command type name: ' || command);
IF command = 'INSERT' THEN
rowlcr.SET_COMMAND_TYPE('DELETE');
new_values := rowlcr.GET_VALUES('new');
-- Set the old values in the row LCR to the new values in the row LCR
rowlcr.SET_VALUES('old', new_values);
-- Set the old values in the row LCR to NULL
rowlcr.SET_VALUES('new', NULL);
old_values := rowlcr.GET_VALUES('old');
-- Apply the row LCR as an DELETE FROM the table
object_name :=rowlcr.GET_OBJECT_NAME();
object_owner :=rowlcr.GET_OBJECT_OWNER();
key_column := myvarray_list(); --init array.
i :=1;
FOR emm IN (select DISTINCT CO.COLUMN_NAME from
DBA_cons_columns CO, dba_constraints PK
where CO.constraint_name = PK.constraint_name
AND CO.OWNER = PK.OWNER
AND PK.constraint_type IN ('P','C','U')
and PK.table_name = object_name
AND PK.OWNER=object_owner) LOOP
DBMS_OUTPUT.PUT_LINE('key column ' || emm.COLUMN_NAME);
key_column.extend;
key_column(i) := emm.COLUMN_NAME;
i :=i+1;
END LOOP;
remove_column := myvarray_list(); --init array.
remove_count := 1;
FOR i in 1..old_values.count LOOP
IF old_values(i) IS NOT NULL THEN
--DBMS_OUTPUT.PUT_LINE('old(' || i || '): ' || old_values(i).column_name);
x :=1;
remove_flag :=1;
WHILE x <= key_column.count AND remove_flag=1 loop
--dbms_output.put_line('key_column('||x||')='||key_column(x));
IF old_values(i).column_name = key_column(x) THEN
remove_flag :=0;
END IF;
x :=x +1;
END loop;
IF remove_flag = 1 then
remove_column.extend;
remove_column(remove_count) := old_values(i).column_name;
remove_count :=remove_count+1;
END IF;
END IF;
END LOOP;
FOR x in 1..remove_column.count loop
dbms_output.put_line('remove_column('||x||')='||remove_column(x));
rowlcr.DELETE_COLUMN(remove_column(x),'old');
END loop;
rowlcr.EXECUTE(true);
END IF;
BEGIN
--dbms_apply_adm.execute_all_errors(applyname);
dbms_apply_adm.EXECUTE_ERROR(ltxnid);
return;
EXCEPTION when OTHERS then
SELECT MESSAGE_NUMBER INTO i FROM DBA_APPLY_ERROR WHERE LOCAL_TRANSACTION_ID = ltxnid;
v_code := SQLCODE;
v_errm := SUBSTR(SQLERRM, 1, 1024);
DBMS_OUTPUT.PUT_LINE('Error message: ' || v_errm);
IF loopdog > msgcnt then
RAISE_APPLICATION_ERROR(-20002,'Insert or Delete error. please check your procedure.');
ELSIF v_code = -1 then
DBMS_OUTPUT.PUT_LINE('Error code(-1): ' || v_code);
--null;
ELSIF v_code = -26786 then
DBMS_OUTPUT.PUT_LINE('Error code(-26786): ' || v_code);
RAISE_APPLICATION_ERROR(-20786,v_errm);
ELSIF v_code = -26787 then
DBMS_OUTPUT.PUT_LINE('Error code(-26787): ' || v_code);
RAISE_APPLICATION_ERROR(-20787,v_errm);
ELSE
RAISE_APPLICATION_ERROR(-20000,v_errm);
END IF;
END;
END IF;
END LOOP; --loop END EXECUTE_TRANSACTION_1;
--需要这两个 权限
--grant select on dba_constraints to DWESBSTREAMUSER;
--granT select on DBA_cons_columns to DWESBSTREAMUSER;

ORA-00001 错误是目标端存在数据,但是存在冲突列, 这里储存储过程的思想是根据唯一列删除目标端的对应行, 并将LCR 是old 值插入到目标端.

-----------------------------------------------------------------------------------------------------------------------------------

CREATE OR REPLACE PROCEDURE EXECUTE_TRANSACTION_26786(applyname IN VARCHAR2,ltxnid IN VARCHAR2) IS
i NUMBER;
x NUMBER;
loopdog NUMBER;
txnid VARCHAR2(30);
source VARCHAR2(128);
msgno NUMBER;
msgcnt NUMBER;
errno NUMBER;
errmsg VARCHAR2(2000);
lcr ANYDATA;
rowlcr SYS.LCR$_ROW_RECORD;
typenm VARCHAR2(61);
res NUMBER;
command VARCHAR2(10);
old_values SYS.LCR$_ROW_LIST;
new_values SYS.LCR$_ROW_LIST;
v_code NUMBER;
v_errm VARCHAR2(1024);
object_owner VARCHAR2(30);
object_name VARCHAR2(40);
key_column myvarray_list;
remove_column myvarray_list;
remove_flag NUMBER;
remove_count NUMBER;
BEGIN
SELECT LOCAL_TRANSACTION_ID,
SOURCE_DATABASE,
MESSAGE_NUMBER,
MESSAGE_COUNT,
ERROR_NUMBER,
ERROR_MESSAGE
INTO txnid, source, msgno, msgcnt, errno, errmsg
FROM DBA_APPLY_ERROR
WHERE LOCAL_TRANSACTION_ID = ltxnid;
DBMS_OUTPUT.PUT_LINE('--- Local Transaction ID: ' || txnid);
DBMS_OUTPUT.PUT_LINE('--- Source Database: ' || source);
DBMS_OUTPUT.PUT_LINE('---Error in Message: '|| msgno);
DBMS_OUTPUT.PUT_LINE('---Error Number: '||errno);
DBMS_OUTPUT.PUT_LINE('---Message Text: '||errmsg);
i := msgno;
loopdog :=0;
WHILE i <= msgcnt LOOP
loopdog :=loopdog+1;
DBMS_OUTPUT.PUT_LINE('---message: ' || i);
lcr := DBMS_APPLY_ADM.GET_ERROR_MESSAGE(i, txnid); -- gets the LCR
typenm := lcr.GETTYPENAME();
DBMS_OUTPUT.PUT_LINE('type name: ' || typenm);
IF (typenm = 'SYS.LCR$_ROW_RECORD') THEN
res := lcr.GETOBJECT(rowlcr);
command := rowlcr.GET_COMMAND_TYPE();
DBMS_OUTPUT.PUT_LINE('command type name: ' || command);
IF command IN ('DELETE','UPDATE') THEN
rowlcr.SET_COMMAND_TYPE('DELETE');
old_values := rowlcr.GET_VALUES('old');
-- Set the new values in the row LCR to NULL
rowlcr.SET_VALUES('new', NULL);
-- Apply the row LCR as an DELETE FROM the table --
object_name :=rowlcr.GET_OBJECT_NAME();
object_owner :=rowlcr.GET_OBJECT_OWNER();
---------------------------------------------------------------------------------------
key_column := myvarray_list(); --init array.
i :=1;
FOR emm IN (select DISTINCT CO.COLUMN_NAME from
DBA_cons_columns CO, dba_constraints PK
where CO.constraint_name = PK.constraint_name
AND CO.OWNER = PK.OWNER
AND PK.constraint_type IN ('P','C','U')
and PK.table_name = object_name
AND PK.OWNER=object_owner) LOOP
DBMS_OUTPUT.PUT_LINE('key column ' || emm.COLUMN_NAME);
key_column.extend;
key_column(i) := emm.COLUMN_NAME;
i :=i+1;
END LOOP;
-----------------------------------------------------------------------------------
remove_column := myvarray_list(); --init array.
remove_count := 1;
FOR i in 1..old_values.count LOOP
IF old_values(i) IS NOT NULL THEN
--DBMS_OUTPUT.PUT_LINE('old(' || i || '): ' || old_values(i).column_name);
x :=1;
remove_flag :=1;
WHILE x <= key_column.count AND remove_flag=1 loop
--dbms_output.put_line('key_column('||x||')='||key_column(x));
IF old_values(i).column_name = key_column(x) THEN
remove_flag :=0;
END IF;
x :=x +1;
END loop;
IF remove_flag = 1 then
remove_column.extend;
remove_column(remove_count) := old_values(i).column_name;
remove_count :=remove_count+1;
END IF;
END IF;
END LOOP; -----------------------------------------------------------------------------
FOR x in 1..remove_column.count loop
dbms_output.put_line('remove_column('||x||')='||remove_column(x));
rowlcr.DELETE_COLUMN(remove_column(x),'old');
END loop;
rowlcr.EXECUTE(true);
DBMS_OUTPUT.PUT_LINE('DELETE target old new ');
--- INSERT NEW DATA--
rowlcr.SET_COMMAND_TYPE('INSERT');
rowlcr.SET_VALUES('new',old_values);
rowlcr.SET_VALUES('old',NULL);
rowlcr.EXECUTE(true);
DBMS_OUTPUT.PUT_LINE('insert source old new');
END IF;
BEGIN
--dbms_apply_adm.execute_all_errors(applyname);
dbms_apply_adm.EXECUTE_ERROR(ltxnid);
return;
EXCEPTION when OTHERS then
SELECT MESSAGE_NUMBER INTO i FROM DBA_APPLY_ERROR WHERE LOCAL_TRANSACTION_ID = ltxnid;
v_code := SQLCODE;
v_errm := SUBSTR(SQLERRM, 1, 1024);
DBMS_OUTPUT.PUT_LINE('Error message: ' || v_errm);
IF loopdog > msgcnt then
RAISE_APPLICATION_ERROR(-20002,'Insert or Delete error. please check your procedure.');
ELSIF v_code = -26786 then
DBMS_OUTPUT.PUT_LINE('Error code(-26786): ' || v_code);
ELSIF v_code = -26787 then
DBMS_OUTPUT.PUT_LINE('Error code(-26787): ' || v_code);
RAISE_APPLICATION_ERROR(-20787,v_errm);
ELSIF v_code = -1 then
DBMS_OUTPUT.PUT_LINE('Error code(-1): ' || v_code);
RAISE_APPLICATION_ERROR(-20001,v_errm);
ELSE
RAISE_APPLICATION_ERROR(-20000,v_errm);
END IF;
END;
END IF;
END LOOP; END EXECUTE_TRANSACTION_26786;

ORA-26786 思想与 ORA-00001 思想类似, 不再赘述.

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

CREATE OR REPLACE PROCEDURE EXECUTE_TRANSACTION_26787(applyname IN VARCHAR2,ltxnid IN VARCHAR2) IS
i NUMBER;
loopdog NUMBER;
txnid VARCHAR2(30);
source VARCHAR2(128);
msgno NUMBER;
msgcnt NUMBER;
errno NUMBER;
errmsg VARCHAR2(2000);
lcr ANYDATA;
rowlcr SYS.LCR$_ROW_RECORD;
typenm VARCHAR2(61);
res NUMBER;
command VARCHAR2(10);
old_values SYS.LCR$_ROW_LIST;
new_values SYS.LCR$_ROW_LIST;
v_code NUMBER;
v_errm VARCHAR2(1024);
BEGIN
SELECT LOCAL_TRANSACTION_ID,
SOURCE_DATABASE,
MESSAGE_NUMBER,
MESSAGE_COUNT,
ERROR_NUMBER,
ERROR_MESSAGE
INTO txnid, source, msgno, msgcnt, errno, errmsg
FROM DBA_APPLY_ERROR
WHERE LOCAL_TRANSACTION_ID = ltxnid;
DBMS_OUTPUT.PUT_LINE('--- Local Transaction ID: ' || txnid);
DBMS_OUTPUT.PUT_LINE('--- Source Database: ' || source);
DBMS_OUTPUT.PUT_LINE('---Error in Message: '|| msgno);
DBMS_OUTPUT.PUT_LINE('---Error Number: '||errno);
DBMS_OUTPUT.PUT_LINE('---Message Text: '||errmsg);
i := msgno;
loopdog :=0;
WHILE i <= msgcnt LOOP
loopdog :=loopdog+1;
DBMS_OUTPUT.PUT_LINE('--message: ' || i);
lcr := DBMS_APPLY_ADM.GET_ERROR_MESSAGE(i, txnid); -- gets the LCR
--print_lcr(lcr);
typenm := lcr.GETTYPENAME();
DBMS_OUTPUT.PUT_LINE('type name: ' || typenm);
IF (typenm = 'SYS.LCR$_ROW_RECORD') THEN
res := lcr.GETOBJECT(rowlcr);
command := rowlcr.GET_COMMAND_TYPE();
DBMS_OUTPUT.PUT_LINE('command type name: ' || command);
IF command = 'DELETE' THEN
-- Set the command_type in the row LCR to INSERT
rowlcr.SET_COMMAND_TYPE('INSERT');
old_values := rowlcr.GET_VALUES('old');
-- Set the old values in the row LCR to the new values in the row LCR
rowlcr.SET_VALUES('new', old_values);
-- Set the old values in the row LCR to NULL
rowlcr.SET_VALUES('old', NULL);
-- Apply the row LCR as an INSERT into the hr.emp_del table
rowlcr.EXECUTE(true);
ELSIF command = 'UPDATE' THEN
BEGIN
old_values := rowlcr.GET_VALUES('old');
new_values := rowlcr.GET_VALUES('new');
rowlcr.EXECUTE(true);
rowlcr.SET_VALUES('new', old_values);
rowlcr.SET_VALUES('old', new_values);
rowlcr.EXECUTE(true);
EXCEPTION when OTHERS then
rowlcr.SET_COMMAND_TYPE('INSERT');
rowlcr.SET_VALUES('new', old_values);
-- Set the old values in the row LCR to NULL
rowlcr.SET_VALUES('old', NULL);
rowlcr.EXECUTE(true);
END;
END IF;
BEGIN
--dbms_apply_adm.execute_all_errors(applyname);
dbms_apply_adm.execute_error(ltxnid);
return;
EXCEPTION when OTHERS then
SELECT MESSAGE_NUMBER INTO i FROM DBA_APPLY_ERROR WHERE LOCAL_TRANSACTION_ID = ltxnid;
v_code := SQLCODE;
v_errm := SUBSTR(SQLERRM, 1, 1024);
DBMS_OUTPUT.PUT_LINE('Error message: ' || v_errm);
IF loopdog > msgcnt then
RAISE_APPLICATION_ERROR(-20002,'Insert or Delete error. please check your procedure.');
ELSIF v_code = -26787 then
DBMS_OUTPUT.PUT_LINE('Error code(-26787): ' || v_code);
--null;
ELSIF v_code = -26786 then
DBMS_OUTPUT.PUT_LINE('Error code(-26786): ' || v_code);
RAISE_APPLICATION_ERROR(-20786,v_errm);
ELSIF v_code = -1 then
DBMS_OUTPUT.PUT_LINE('Error code(-1): ' || v_code);
RAISE_APPLICATION_ERROR(-20001,v_errm);
ELSE
RAISE_APPLICATION_ERROR(-20001,v_errm);
END IF;
END;
END IF;
END LOOP; END EXECUTE_TRANSACTION_26787;

ORA-26787  处理方法相对简单, 因为数据不存在引起的, 只要把LCR 中old 字段插入即可.

参考 :

-- https://docs.oracle.com/cd/B28359_01/server.111/b28321/strms_apmon.htm

-- https://docs.oracle.com/cd/B10501_01/server.920/a96571/capappdemo.htm

-- https://docs.oracle.com/cd/B19306_01/appdev.102/b14258/t_lcr.htm#i997627

Stream 同步错误之解决方案 ORA-00001 ORA-26787 ORA-26786的更多相关文章

  1. Oracle启动中,spfile.ora、init<SID>.ora、spfile<SID>.ora 这三个文件正确的先后顺序是什么?

    Oracle启动中,spfile.ora.init<SID>.ora.spfile<SID>.ora 这三个文件正确的先后顺序是什么? 解答:启动数据库,使用startup命令 ...

  2. oracle--本地网络配置tnsnames.ora和监听器listener.ora

    文件tnsnames.ora 是给orcl客户端使用 配置本地网络服务:(客户端) 第一种使用暴力方式直接操作: 修改:C:\app\Administrator\product\11.2.0\dbho ...

  3. Oracle的tnsnames.ora配置(PLSQL Developer)

    首先打开tnsnames.ora的存放目录,一般为D:\app\Administrator\product\11.2.0\client_1\network\admin,就看安装具体位置了. 步骤阅读 ...

  4. listener.ora/sqlnet.ora/tnsnames.ora配置文件详解

    oracle网络配置 三个配置文件 listener.ora.sqlnet.ora.tnsnames.ora ,都是放在$ORACLE_HOME/network/admin目录下. 英文说明: The ...

  5. Oracle的sqlnet.ora与password文件试验

    先看有没有sqlnet.ora [oracle@localhost ~]$ cd $ORACLE_HOME[oracle@localhost dbhome_1]$ cd network[oracle@ ...

  6. 转载《Oracle的tnsnames.ora配置(PLSQL Developer)》

    源地址:https://www.cnblogs.com/qq3245792286/p/6212617.html. 首先打开tnsnames.ora的存放目录,一般为D:\app\Administrat ...

  7. [转帖]sqlnet.ora常用参数

    sqlnet.ora常用参数 注﹕在修改sqlnet.ora文件之后重新启动监听﹐修改才能生效﹗﹗﹗ oracle网络设置主要包括三个文件,sqlnet.ora\ lisnter.ora\ tnsna ...

  8. PLSQL连接ORACLE配置字符串简介 oracle网络配置 三个配置文件 listener.ora、sqlnet.ora、tnsnames.ora原理解释

    PLSQL连接ORACLE配置字符串简介 oracle网络配置 三个配置文件 listener.ora.sqlnet.ora.tnsnames.ora原理解释 oracle网络配置三个配置文件 lis ...

  9. oracle安装完成后目录中不论有没有tnsnames.ora和listener.ora文件 PLSQL都能连上的问题解决方法

    今天遇到这个问题了,发现listener.ora文件和tnsnames.ora文件在Net Work文件夹下没有,正常情况下安装完oracle或者是oracle Client是会有的,但是在Net M ...

随机推荐

  1. UVA 1151二进制枚举子集 + 最小生成树

    题意:平面上有n个点(1<=N<=1000),你的任务是让所有n个点连通,为此, 你可以新建一些边,费用等于两个端点的欧几里得距离的平方.另外还有q(0<=q<=8)个套餐(数 ...

  2. Math类常用方法(Java)

    三角函数: public static double sin (double radians) public static double cos(double radians) public stat ...

  3. 【.net部署】Server Error in '/' Application.错误解决方案

    报错: Server Error in '/' Application.---------------------------------------------------------------- ...

  4. Nginx反向代理配置可跨域

    由于业务需要,同一项目中的前端代码放在静态环境中,而后端代码放在tomcat中,但此时问题却出现了:前端使用ajax请求后端获取数据时出现如下报错 XMLHttpRequest cannot load ...

  5. 加密web.config

    当我们要进行数据库的连接时,就会根据<%$ connectionsStrings:MyConnectionStringName %>这个表达式在Web.config文件中找到和MyConn ...

  6. display:inline; display:block;

    block(块级元素): div .from. p .table. pre.h1~h6. dl .ol .ul等 inline(内联元素): span.a.strong.em.label.input. ...

  7. 行列式计算(C#)

    最近几天学习高等代数老师说要写个程序算行列式的结果,闲来无事就简单写了一下. 不多说了,上代码 using System; using System.Collections.Generic; usin ...

  8. SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT 'OpenRowset/OpenDatasource' 的访问

    消息 15281,级别 16,状态 1,第 2 行SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT 'OpenRowset/Open ...

  9. ImageLoader框架的使用、调用系统相册显示图片并裁剪显示、保存图片的两种方式

    ImageLoader虽然说是一个相对于比较老的一个框架了 ,但是总的来说,还是比较好用的,今天我就总结了一下它的用法.还有调用系统相册并裁剪,以及,通过sharedpreference和文件存储来保 ...

  10. IOS tableview下拉刷新上拉加载分页

    http://code4app.com/ios/快速集成下拉上拉刷新/52326ce26803fabc46000000 刷新没用用插件,加载使用的MJ老师的插件. - (void)viewDidLoa ...