温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Oracle自治事务autonomous_transaction怎么用

发布时间:2021-11-11 14:07:46 来源:亿速云 阅读:436 作者:小新 栏目:关系型数据库

这篇文章主要为大家展示了“Oracle自治事务autonomous_transaction怎么用”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“Oracle自治事务autonomous_transaction怎么用”这篇文章吧。

范例
1. 过程中的使用:

非自治事務如下

create or replace procedure sfis1.nonautonomous_insert_goal

as

begin

    insert into t values('NONAUTONOMOUS INSERT','1','1');

    commit;

end;

自治事務如下

create or replace procedure sfis1.autonomous_insert_goal

as pragma autonomous_transaction;

begin

    insert into t values('AUTONOMOUS INSERT','1','1');

    commit;

end;

當執行如下(插入兩行):

begin

insert into t values ('AUTONOMOUS INSERT','1','1');

NONAUTONOMOUS_INSERT_GOAL;

rollback;

end;

當執行下面(只插入一行)

begin

insert into t values ('AUTONOMOUS INSERT','1','1');

AUTONOMOUS_INSERT_GOAL;

rollback;  end;

 2.trigger 中的使用:

 觸發器只commit該觸發的信息,觸發器之外的不會commit

create table emp

as

select * from scott.emp;

create table audit_tab

( username varchar2(30) default user,

timestamp date default sysdate,

msg varchar2(4000))

 
只有他的老板才能更新他的信息

create or replace trigger sfis1.emp_audit

before update on sfis1.emp for each row

declare

pragma autonomous_transaction;

l_cnt number;

begin

select count(*) into l_cnt from dual

where exists ( select null from emp where empno = :new.empno

start with mgr = ( select empno from emp where ename = user )

connect by prior empno = mgr );

 if ( l_cnt = 0 )

 then

 insert into audit_tab ( msg )values ('Attempt to update'|| :new.empno );

 commit;

 raise_application_error(-20001,'Access Denied');

 end if;

 end;

3.對对select查询语句的审计方法

create table sfis1.audit_trail

(username varchar2(30 byte), pk number,attribute varchar2(30 byte),

 Dataum varchar2(255 byte),timestamp  date)

查詢及記錄用視圖,當查詢含sal, comm, hiredate記錄時插入審計表

create or replace force view sfis1.emp_v

(empno, ename, job, mgr, sal, comm, hiredate, deptno)

as

   select empno, ename, job, mgr,

        audit_trail_pkg.record (empno, 'sal', sal) sal,

        audit_trail_pkg.record (empno, 'comm', comm) comm,

audit_trail_pkg.record (empno, 'hiredate', hiredate) hiredate,

deptno from   emp;

CREATE OR REPLACE package SFIS1.audit_trail_pkg

as

function record( p_pk in number,p_attr in varchar2,p_dataum in number ) return number;

function record( p_pk in number,p_attr in varchar2,p_dataum in varchar2 ) return varchar2;

function record( p_pk in number,p_attr in varchar2,p_dataum in date ) return date;

end;

create or replace package body sfis1.audit_trail_pkg

as

procedure log( p_pk in number,p_attr in varchar2,p_dataum in varchar2 )

as

pragma autonomous_transaction;

begin

    insert into audit_trail values( user, p_pk, p_attr, p_dataum, sysdate );

    commit;

end;

function record( p_pk in number,p_attr in varchar2,p_dataum in number ) return number

is

begin

    log( p_pk, p_attr, p_dataum );

    return p_dataum;

end;

function record( p_pk in number,p_attr in varchar2,p_dataum in varchar2 ) return varchar2

is

begin

    log( p_pk, p_attr, p_dataum );

    return p_dataum;

end;

function record( p_pk in number,p_attr in varchar2,p_dataum in date ) return date

is

begin

    log( p_pk, p_attr,to_char(p_dataum,'dd.mon.yyyy hh34:mi:ss') );

    return p_dataum;

end;

end;

 
结束自治事务:
为了结束自治事务,必须发出一个完成的COMMIT或ROLLBACK,或执行DDL(隐含COMMIT)。當自主事務做出SET TRANSACTION或SAVEPOINT時,自主事務將自動開始。

以上是“Oracle自治事务autonomous_transaction怎么用”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI