上一篇博客中(http://blog.csdn.net/ysc1123/article/details/50721592)我们对Oracle的情况进行了基本了解,今天小编就用实例说话,让大家对自治事务的理解更加深刻:
触发无法包含COMMIT语句,除非有PRAGMAAUTONOMOUS_TRANSACTION标记。但是,只有触发中的语句才能被提交,主事务则不行。
现在我们sql语句: Create table Msg (Msg varchar(50)) ; 创建了表Msg;
首先我们使用自治事务:
create or replace procedure AutoNomouse_Insert is
PRAGMA AUTONOMOUS_TRANSACTION;
begin
insert into Msg values('AutoNomouse Insert');
commit;
end;
再使用
非自治事务:
CREATE OR REPLACE Procedure NonAutoNomouse_Insert as
begin
insert into Msg Values('NonAutonomouse Insert');
commit;
end;
SQL> begin
2
3 insert into Msg Values('This Main Info');
4
5 NonAutoNomouse_Insert;
6
7 rollback;
8
9 end
10 ;
11 /
PL/SQL procedure successfully completed
SQL> select * from msg;
MSG
--------------------------------------------------
This Main Info
NonAutonomouse Insert
由于过程中欧commit,
所以匿名块中的R
O
LLBACK是不起作用的; 由此得出:非自治事务中的COMMIT,ROLLBACK是会影响整个事务的。
下面我们看一下另一种情况:
<span style="font-family:SimSun;">SQL> delete msg;
2 rows deleted
SQL>
</span>
(注意: 这里并没有commit)
<span style="font-family:SimSun;">SQL> begin
2
3 insert into Msg Values('This Main Info');
4
5 rollback; --这里加了ROLLBACK;
6
7 NonAutoNomouse_Insert;
8
9 rollback;
10
11 end
12 ;
13 /
PL/SQL procedure successfully completed
SQL> select * from msg;
MSG
--------------------------------------------------
This Main Info
NonAutonomouse Insert
NonAutonomouse Insert
</span>
从上面的结果可以发现,delete from ssg 并没有ROLL BACK,为什么? 因为过程就是一个新的SESSION,所以前面(也就是delete)的SESSION被正常EXIT,同时被自动提交;所以我们会看到三行数据。
到这里,oracle的自治事务就基本上介绍完了,不知道小编有没有带领好大家去认识和深入了解oracle的自治事务呢?以上均为个人见解,如果更多建议或者见解的,欢迎交流!