事务与锁定-嵌套事务和自治事务

/*
drop table  emp; --if exists?
create table emp(
	empid number(5),
	empname varchar2(100),
	empage number(5)
);
*/
--可以通过 PRAGMA AUTONOMOUS_TRANSACTION;来声明当前事务为自治事务
--可以独立对本事务进行提交,不会外层事务。及时外层事务没有提交,自治事务也是可以被提交的。
delete from emp;
--定义一个嵌套的自治事务(存储过程)
create or replace procedure insertchenzw
as
	PRAGMA AUTONOMOUS_TRANSACTION;
begin
	insert into emp values(1,'chenzw',27);
	commit;
end;
/
--定义一个执行的存储过程:
create or replace procedure testtransaction
as
begin
	insert into emp values(2,'chenzz',27);
	insertchenzw;
	insert into emp values(2,'chenzz',27);
	rollback;
end;
/

execute testtransaction;
col empname format a20;
select * from emp;

drop procedure insertchenzw;
drop procedure testtransaction;

--示例程序执行如下:


SQL> create or replace procedure insertchenzw
  2  as
  3   PRAGMA AUTONOMOUS_TRANSACTION;
  4  begin
  5   insert into emp values(1,'chenzw',27);
  6   commit;
  7  end;
  8  /

过程已创建。

SQL> create or replace procedure testtransaction
  2  as
  3  begin
  4   insert into emp values(2,'chenzz',27);
  5   insertchenzw;
  6   insert into emp values(2,'chenzz',27);
  7   rollback;
  8  end;
  9  /

过程已创建。

SQL> execute testtransaction;

PL/SQL 过程已成功完成。

SQL> col empname format a20;
SQL> select * from emp;

     EMPID EMPNAME                  EMPAGE
---------- -------------------- ----------
         1 chenzw                       27

SQL> 

你可能感兴趣的:(自治事务)