触发器传参数给存储过程,存储过程中有insert tableA where id=1....接着update tableA where id=1.
由于insert语句不是自动提交,所以当insert语句没有commit的时候,update会报“表/视图发生了变化,程序不能读它”的错误,也就是id=1这一行被insert锁住了。
此时,在触发器中调用自治事务,问题得以解决。
create or replace trigger trigger_main2_update
before update on t_busi_main_presend2 for each row
declare
pragma autonomous_transaction; --声明该触发器的事务为自治事务
begin
DBMS_OUTPUT.PUT_LINE(:new.SHSTATUS);
if:new.SHSTATUS='1' and :old.ld.SHSTATUS='0'
then
p_main2_mx(:new.id,:new.smscontent,:new.allcode,:new.phonetype,:new.sjtongdaoid,:new.cjr,:new.pretongdaoid,:new.clientid,:new.shr,:new.pretime,:new.cjsj,:new.shstatus,:new.kouchucnt,:new.dxlx,:new.allcount); --调用存储过程,并给存储过程传参数
:new.SENDSTATUS:='1' ;
end if;
commit;
end;
运用AT(autonomous_transaction)时,有一些注意事项,简单列举如下:
1. 在匿名PL/SQL块中,只有顶级的匿名PL/SQL块可以被设为AT
2. 如果AT试图访问被MT控制的资源,可能有deadlock发生.
3. Package 不能被声明为AT,只有package所拥有的function和procedure 才能声明为AT
4. AT程序必须以commit 或rollback结尾,否则会产生Oracle错误ORA-06519: active autonomous transaction detected and rolled back