Stream 同步错误之解决方案 ORA-00001 ORA-26787 ORA-26786
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的更多相关文章
- Oracle启动中,spfile.ora、init<SID>.ora、spfile<SID>.ora 这三个文件正确的先后顺序是什么?
Oracle启动中,spfile.ora.init<SID>.ora.spfile<SID>.ora 这三个文件正确的先后顺序是什么? 解答:启动数据库,使用startup命令 ...
- oracle--本地网络配置tnsnames.ora和监听器listener.ora
文件tnsnames.ora 是给orcl客户端使用 配置本地网络服务:(客户端) 第一种使用暴力方式直接操作: 修改:C:\app\Administrator\product\11.2.0\dbho ...
- Oracle的tnsnames.ora配置(PLSQL Developer)
首先打开tnsnames.ora的存放目录,一般为D:\app\Administrator\product\11.2.0\client_1\network\admin,就看安装具体位置了. 步骤阅读 ...
- listener.ora/sqlnet.ora/tnsnames.ora配置文件详解
oracle网络配置 三个配置文件 listener.ora.sqlnet.ora.tnsnames.ora ,都是放在$ORACLE_HOME/network/admin目录下. 英文说明: The ...
- Oracle的sqlnet.ora与password文件试验
先看有没有sqlnet.ora [oracle@localhost ~]$ cd $ORACLE_HOME[oracle@localhost dbhome_1]$ cd network[oracle@ ...
- 转载《Oracle的tnsnames.ora配置(PLSQL Developer)》
源地址:https://www.cnblogs.com/qq3245792286/p/6212617.html. 首先打开tnsnames.ora的存放目录,一般为D:\app\Administrat ...
- [转帖]sqlnet.ora常用参数
sqlnet.ora常用参数 注﹕在修改sqlnet.ora文件之后重新启动监听﹐修改才能生效﹗﹗﹗ oracle网络设置主要包括三个文件,sqlnet.ora\ lisnter.ora\ tnsna ...
- PLSQL连接ORACLE配置字符串简介 oracle网络配置 三个配置文件 listener.ora、sqlnet.ora、tnsnames.ora原理解释
PLSQL连接ORACLE配置字符串简介 oracle网络配置 三个配置文件 listener.ora.sqlnet.ora.tnsnames.ora原理解释 oracle网络配置三个配置文件 lis ...
- oracle安装完成后目录中不论有没有tnsnames.ora和listener.ora文件 PLSQL都能连上的问题解决方法
今天遇到这个问题了,发现listener.ora文件和tnsnames.ora文件在Net Work文件夹下没有,正常情况下安装完oracle或者是oracle Client是会有的,但是在Net M ...
随机推荐
- sql语句查询结果添加排序列(转)
给查询出的SQL记录添加序号列,解决方法有以下两种 第一: select ROW_NUMBER() OVER (ORDER BY a.字段 ASC) AS XUHAO,a.* from table ...
- 强大反调试cm的奇葩破解
系统 : Windows xp 程序 : Crackme-xp 程序下载地址 :http://pan.baidu.com/s/1slUwmVr 要求 : 编写注册机 使用工具 : OD & I ...
- Data Big Bang
在过去的五十多年中,我们可以较为直观地看到IT行业正以蓬勃发展之势渗入到我们生活的方方面面.虽经历过几轮新兴和重叠的技术浪潮,但每一波浪潮都伴随着新兴技术的革新.IT供应商主导着互联网的走向,网络秩序 ...
- PHP类和对象之间的关系
类是对象的描述: 类和对象之间的关系类似于建房子的图纸和房子: 创建类--new对象--用对象: 一.对象的两个特性: --对象的行为(功能):可以对对象施加操作,例如,电视机的开.关.转换频道: - ...
- jquery时间控件datepicker
配置文件 $("#joinedTime").datepicker({ inline: true, yearRange: "1996:2016", showBut ...
- 从小工到专家 ——读《Java程序员职场全攻略》有感
从小工到专家 ——读<Java程序员职场全攻略>有感 <Java程序员职场全攻略>是以故事的形式,向读者介绍Java程序员的职场经验.作者牛开复在北京从事软件开发,已经是一 ...
- Please allow Subclipse team to receive anonymous usage statistics for this Eclipse intance(info)
本文转载自:http://blog.csdn.net/myfxx/article/details/21096949 今天在用eclipse启动项目的时候发现了一个问题,就是每次启动项目的时候,ecli ...
- bn
BN是在每一层之前对神经元的输入进行归一化,对sigmoid激活函数有效(对Relu也有效),可以更快的收敛且可以有效减少过拟合.
- Angularjs scope
$scope: var myapp = angular.module('myapp', []); myapp .controller('parent', function ($scope,$timeo ...
- 关于 vmware虚拟机的一些问题及解决办法备忘
有问题讨论 --- 问题:关于vm虚拟机窗口大小全屏按钮无法全屏 解决:安装vm-tools,重启即可 --- 问题:关于vm虚拟机安装xp,尤其还原ghost出错找不到光驱 解决:进入镜像pe安装 ...