PRAGMA AUTONOMOUS_TRANSACTION(自治事务)

来源:互联网 发布:jackson json忽略空值 编辑:程序博客网 时间:2024/05/17 21:45

http://blog.csdn.net/pan_tian/article/details/7675800


这段时间遇到一个问题,程序里明明插入了一条记录,但在后边的一段Procedure中却查不到刚刚插入的记录,最后发现这个Procedure的定义中加入了PRAGMA AUTONOMOUS_TRANSACTION

PRAGMA AUTONOMOUS_TRANSACTION中文翻译过来叫“自治事务”(翻译的还算好理解),对于定义成自治事务的Procedure,实际上相当于一段独立运行的程序段,这段程序不依赖于主程序,也不干涉主程序


自治事务的特点

第一,这段程序不依赖于原有Main程序,比如Main程序中有未提交的数据,那么在自治事务中是查找不到的。

第二,在自治事务中,commit或者rollback只会提交或回滚当前自治事务中的DML,不会影响到Main程序中的DML。


Autonomous Transaction Demo 1

Without Pragma Autonomous Transaction

[sql] view plain copy print?
  1. CREATE TABLE t (  
  2. test_value VARCHAR2(25));  
  3.   
  4. CREATE OR REPLACE PROCEDURE child_block IS  
  5.   
  6. BEGIN  
  7.   INSERT INTO t  
  8.   (test_value)  
  9.   VALUES  
  10.   ('Child block insert');  
  11.   COMMIT;  
  12. END child_block;  
  13. /  
  14.   
  15. CREATE OR REPLACE PROCEDURE parent_block IS  
  16.   
  17. BEGIN  
  18.   INSERT INTO t  
  19.   (test_value)  
  20.   VALUES  
  21.   ('Parent block insert');  
  22.   
  23.    child_block;  
  24.   
  25.    ROLLBACK;  
  26. END parent_block;  
  27. /  
  28.   
  29. -- run the parent procedure  
  30. exec parent_block  
  31.   
  32. -- check the results  
  33. SELECT * FROM t;  
[sql] view plain copy print?
  1. Output:  
  2. Parent block insert  
  3. Child block insert  
With Pragma Autonomous Transaction
[sql] view plain copy print?
  1. CREATE OR REPLACE PROCEDURE child_block IS  
  2.   
  3. PRAGMA AUTONOMOUS_TRANSACTION;  
  4.   
  5. BEGIN  
  6.   INSERT INTO t  
  7.   (test_value)  
  8.   VALUES  
  9.   ('Child block insert');  
  10.   
  11.   COMMIT;  
  12. END child_block;  
  13. /  
  14.   
  15. CREATE OR REPLACE PROCEDURE parent_block IS  
  16.   
  17. BEGIN  
  18.   INSERT INTO t  
  19.   (test_value)  
  20.   VALUES  
  21.   ('Parent block insert');  
  22.   
  23.    child_block;  
  24.   
  25.    ROLLBACK;  
  26. END parent_block;  
  27. /  
  28. -- empty the test table  
  29. TRUNCATE TABLE t;  
  30.   
  31. -- run the parent procedure  
  32. exec parent_block;  
  33.   
  34. -- check the results  
  35. SELECT * FROM t;  
[sql] view plain copy print?
  1. Output:  
  2. Child block insert  

Autonomous Transaction Demo 2

Without Pragma Autonomous Transaction
[sql] view plain copy print?
  1. DROP TABLE t;  
  2.   
  3. CREATE TABLE t (testcol NUMBER);  
  4.   
  5. CREATE OR REPLACE FUNCTION howmanyrows RETURN INTEGER IS  
  6.  i INTEGER;  
  7. BEGIN  
  8.   SELECT COUNT(*)  
  9.   INTO i  
  10.   FROM t;  
  11.   
  12.   RETURN i;  
  13. END howmanyrows;  
  14. /  
  15.   
  16. CREATE OR REPLACE PROCEDURE testproc IS  
  17.  a INTEGER;  
  18.  b INTEGER;  
  19.  c INTEGER;  
  20. BEGIN  
  21.   SELECT COUNT(*)  
  22.   INTO a  
  23.   FROM t;  
  24.   
  25.   INSERT INTO t VALUES (1);  
  26.   COMMIT;  
  27.   
  28.   INSERT INTO t VALUES (2);  
  29.   INSERT INTO t VALUES (3);  
  30.   
  31.   b := howmanyrows;  
  32.   
  33.   INSERT INTO t VALUES (4);  
  34.   INSERT INTO t VALUES (5);  
  35.   INSERT INTO t VALUES (6);  
  36.   COMMIT;  
  37.   
  38.   SELECT COUNT(*)  
  39.   INTO c  
  40.   FROM t;  
  41.   
  42.   dbms_output.put_line(a);  
  43.   dbms_output.put_line(b);  
  44.   dbms_output.put_line(c);  
  45. END testproc;  
  46. /  
  47.   
  48. set serveroutput on  
  49.   
  50. exec testproc  
[sql] view plain copy print?
  1. Output:  
  2. 0  
  3. 3  
  4. 6  
  5. Total execution time 2.782 sec.  
With Pragma Autonomous Transaction
[sql] view plain copy print?
  1. CREATE OR REPLACE FUNCTION howmanyrows RETURN INTEGER IS  
  2.  i INTEGER;  
  3.   
  4.  PRAGMA AUTONOMOUS_TRANSACTION;  
  5. BEGIN  
  6.   SELECT COUNT(*)  
  7.   INTO i  
  8.   FROM t;  
  9.   
  10.   RETURN i;  
  11. END howmanyrows;  
  12. /  
  13.   
  14. -- empty the test table  
  15. TRUNCATE TABLE t;  
  16.   
  17. exec testproc;  
[sql] view plain copy print?
  1. Output:  
  2. 0  
  3. 1  
  4. 6  
转载请注明出处:http://blog.csdn.net/pan_tian/article/details/7675800
0 0
原创粉丝点击