Oracle自治事務autonomous_transaction用法
自治事務執行某些工作(COMMIT,ROLLBACK)後不影響當前的外面執行的事務。
範例
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 hh24:mi:ss') );
return p_dataum;
end;
end;
結束自治事務:
為了結束自治事務,必須發出一個完成的COMMIT或ROLLBACK,或執行DDL(隱含COMMIT)。當自主事務做出SET
TRANSACTION或SAVEPOINT時,自主事務將自動開始。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/25583515/viewspace-2147240/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- oracle自治事務(PRAGMA AUTONOMOUS_TRANSACTION)Oracle
- oracle 自治事務Oracle
- 瞭解Oracle自治事務Oracle
- Oracle 巢狀事務 VS 自治事務Oracle巢狀
- oracle自治事務引起的死鎖Oracle
- Oracle中的自治事務(Autonomous Transaction)Oracle
- Oracle PL/SQL 自治事務的說明OracleSQL
- DB2_自治事務DB2
- PLSQL Language Referenc-PL/SQL靜態SQL-自治事務-控制自治事務SQL
- 在Oracle中使用自治事務儲存日誌表條目(轉)Oracle
- PostgreSQL DBA(68) - 使用DBLink實現自治事務SQL
- 【DDL】DDL的隱式commit和自治事務MIT
- 定時自動啟動任務crontab命令用法
- oracle系統包——dbms_job用法(oracle定時任務)Oracle
- PLSQL Language Referenc-PL/SQL靜態SQL-自治事務(二)SQL
- Oracle自動維護任務Oracle
- 使用自治事務在觸發器中執行DDL語句示例觸發器
- ORACLE WITH AS 用法Oracle
- ORACLE SEQUENCE用法Oracle
- oracle cast 用法OracleAST
- oracle job用法Oracle
- oracle restrice用法OracleREST
- oracle10g自動分析任務Oracle
- ORACLE 11G 自動維護任務Oracle
- Oracle EXPLAIN PLAN用法OracleAI
- oracle merge into用法Oracle
- 關於oracle with as用法Oracle
- Oracle sql trace用法OracleSQL
- Oracle Hints的用法Oracle
- oracle job的用法Oracle
- oracle exp imp 用法Oracle
- oracle connect by用法Oracle
- Oracle keep的用法Oracle
- oracle job 用法2Oracle
- Oracle Analyze的用法Oracle
- oracle expdp/impdp用法Oracle
- oracle中top用法Oracle
- Oracle中with的用法Oracle