autonomous transaction是事务中的事务,它能独立于父事务commit或rollback,而不影响当前事务的状态
举例说明:
SQL> CREATE TABLE T(NAME VARCHAR2(20));
SQL>CREATE OR REPLACE PROCEDURE PROC_AUTONOMOUS_TRANSACTION
AS
PRAGMA AUTONOMOUS_TRANSACTION;
BEGIN
INSERT INTO T VALUES('LIUZHI');
COMMIT;
END;
/
SQL>CREATE OR REPLACE PROCEDURE PROC_NORMAL_TRANSACTION
AS
BEGIN
INSERT INTO T VALUES('LIUZHI');
COMMIT;
END;
/
创建两个pl/sql块,分别使用上面的两个procedure。
1 CALL THE PROCEDURE WITH AUTONOMOUS_TRANSACTION PROCEDURE
SQL> BEGIN
INSERT INTO T VALUES('JANDISON');
PROC_AUTONOMOUS_TRANSACTION; -- AUTONOMOUS TRANSACTION
ROLLBACK;
END;
/
查询T表中的数据:
SQL>SELECT * FROM T ;
结果只有:LIUZHI
2 CALL THE PROCEDURE WITHOUT AUTONOMOUS_TRANSACTION PROCEDURE JUST NORMAL_TRANSACTION
SQL> DELETE FROM T;
SQL> COMMIT;
SQL>BEGIN
INSERT INTO T VALUES('JANDISON');
PROC_NORMAL_TRANSACTION; -- NORMAL TRANSACTION
ROLLBACK;
END;
/
查询T表中的数据:
SQL> SELECT * FROM T;
结果为:
JANDISON
LIUZHI
以上的现象清晰地说明了自治事务的commit操作并不影响当前事务的状态,其只commit自身的内容。
AMAZING... ...