在官方文档中,是这样的定义的“Autonomous transactions are independent transactions that can be called from within another transaction.”就是说它被一个事务调用的事务,但它独立于它的父事务提交或回滚。
自治事务的应用场景:
a 匿名块
b 本地、对立或打包的函数或过程
c 对象类型的方法
d 触发器
例如在存储过程中,使用自治事务,事务的提交或者回滚不会对父事务造成影响,同样的父事务的操作也不会影响到自治事务。
自治事务的使用:pragma autonomous_transaction,下例是创建自治事务的存储过程:
create or replace procedure log_err(errinfo varchar2)
as
pragma autonomous_transaction;
begin
insert into test1_log values(systimestamp, errinfo);
commit;
end;
自治事务的应用:记录错误日志或者消息(某一个记录出错可能会需要事务回滚,但是要插入错误日志,使用自治事务,操作回滚但是错误日志正常写到数据库)