以前看同事写的存储过程时,发现经常会在代码的一些分支语句中使用自定义的过程写日志,这个习惯非常好,很方便日常的分析和调试。
点进去看自定义的写日志过程,发现 有 commit 和rollback
如下
--写日志
procedure write_log(l_procedures_name in kk_base_syn_log.procedures_name%type,
l_status in kk_base_syn_log.status%type,
l_log_text in kk_base_syn_log.log_text%type)
is
begin
insert into run_log
(procedures_name, status, log_text)
values
('deal ' || l_procedures_name,
l_status,
'【' || l_procedures_name || '】_数据同步_' || l_log_text);
commit;
exception
when others then
rollback;
end write_log;
这样写是会破坏事务的一致性,查询oracle的官方文档后了解到oracle有一种事务类型叫 AUTONOMOUS TRANSACTION(自治事务)
只需要在 is 和begin 之间添加 PRAGMA AUTONOMOUS_TRANSACTION; 即可。
--写日志
procedure write_log(l_procedures_name in kk_base_syn_log.procedures_name%type,
l_status in kk_base_syn_log.status%type,
l_log_text in kk_base_syn_log.log_text%type)
is
PRAGMA AUTONOMOUS_TRANSACTION;
begin
insert into run_log
(procedures_name, status, log_text)
values
('deal ' || l_procedures_name,
l_status,
'【' || l_procedures_name || '】_数据同步_' || l_log_text);
commit;
exception
when others then
rollback;
end write_log;