看看在oracle中一个事务调用另外一个事务的情况:
事务A:
Begin
DML;
事务B
End;
事务B:
Begin
Commit;
End;
事务A对事务B中的操作可见,如果在事务B充commit,会将事务A中的DML也commit掉,自治事务就是来解决这样的问题的,使得事务B中的提交或回滚对A不可见。
使用一个例子来对比一下自治事务和普通的事务的不同:
SQL> create or replace procedure pro_test_tran is
2 begin
3 commit;
4 end;
5 /
Procedure created
使用pragma autonomous_transaction;来标识使用自治事务
SQL>
SQL> create or replace procedure pro_test_auto is
2 pragma autonomous_transaction;
3 begin
4 commit;
5 end;
6 /
Procedure created
SQL> select a.sal from scott.emp a where a.empno = 7369;
SAL
------------
812.60
SQL> begin
2 update scott.emp a set a.sal = a.sal + .01 where a.empno = 7369;
3 pro_test_auto;
4 rollback;
5 end;
6 /
PL/SQL procedure successfully completed
调用了pro_test_auto以后并没有将父事务中的dml提交掉:
SQL> select a.sal from scott.emp a where a.empno = 7369;
SAL
------------
812.60
SQL>
SQL> begin
2 update scott.emp a set a.sal = a.sal + .01 where a.empno = 7369;
3 pro_test_tran;
4 rollback;
5 end;
6 /
PL/SQL procedure successfully completed
调用了pro_test_ tran将父事务中的dml提交掉了:
SQL> select a.sal from scott.emp a where a.empno = 7369;
SAL
------------
812.61