Welcome 微信登录

首页 / 数据库 / MySQL / Oracle嵌套事务(Nested Transaction)与自治事务(Autonomous Transaction)详解

一. 概念 
1. 嵌套事务(Nested Transaction): 
指在一个Parent事务中嵌套的一个或多个Sub Transaction.并且主事务与其相互影响,这种事务就称为嵌套事务。以Commit作为事务的结束。 
2. 自治事务(Autonomous Transaction): 
指在function,procedure等subprograms中对事务进行自治管理,当在别的pl/sql block里去调用这些subprograms的时候这些subprograms并不随着父pl/sql block的失败而回滚,而是自己管自己commit。以Commit作为事务的结束。自治事务常用于写入LOG或TRAC信息便于查找错误。 

二. 嵌套事务的运用(Nested Transaction) 
1.预备Create Table:
  1. create table TEST_POLICY   
  2. (   
  3.     POLICY_CODE VARCHAR2(20),   
  4.     POLICY_TYPE CHAR(1)   
  5. ) 

2.创建一个嵌套事务的procedure:
  1. Procedure P_Insert_Policy(I_Policy_code varchar2(20),    
  2.                                 I_Policy_type char(1)) as    
  3.       cnt number :=0;   
  4.       begin   
  5.           select count(1) into cnt from Test_Policy;   
  6.           Dbms_Output.put_line("records of the test_policy is "|| cnt);   
  7.              
  8.           Insert into Test_Policy values(I_Policy_code, I_Policy_type);   
  9.           commit;--commit in nested transaction    
  10.       end P_Insert_Policy;   
  11.     --call procedure used in nested transaction   
  12.       PROCEDURE TEST_PL_SQL_ENTRY(   
  13.                                   I_POL_ID IN VARCHAR2,   
  14.                                   O_SUCC_FLG OUT VARCHAR2) AS   
  15.       strSql varchar2(500);   
  16.       cnt number := 0;   
  17.       BEGIN   
  18.          delete from test_policy;   
  19.          commit;   
  20.          insert into test_policy values("2010042101", "1");   
  21.          select count(1) into cnt from Test_Policy;   
  22.          Dbms_Output.put_line("records of the test_policy is "|| cnt);   
  23.          --call nested transaction   
  24.          P_Insert_Policy("2010042102", "2");   
  25.          rollback;--rollback data for all transactions   
  26.          commit;--master transaction commit   
  27.          select count(1) into cnt from Test_Policy;   
  28.          Dbms_Output.put_line("records of the test_policy is "|| cnt);   
  29.          rollback;   
  30.             
  31.          select count(1) into cnt from Test_Policy;   
  32.          Dbms_Output.put_line("records of the test_policy is "|| cnt);   
  33.             
  34.     END TEST_PL_SQL_ENTRY;   
  35.        
  36.     =>run Pl/sql:   
  37.     records of the test_policy is 1 –-主事务中的操作已经commit   
  38.     records of the test_policy is 1 –-主事务的操作对Nested transaction有影响。   
  39.     records of the test_policy is 2 –-Nested transaction 已经Commit   
  40.     records of the test_policy is 2 –-Nested transaction对主事务有影响。  

将上面的nested transaction的procedure修改一下,不需要commit:
  1. Procedure P_Insert_Policy(I_Policy_code T_contract_master.Policy_Code%type,    
  2.                                 I_Policy_type t_contract_master.policy_type%type) as    
  3.       cnt number :=0;   
  4.       begin   
  5.           select count(1) into cnt from Test_Policy;   
  6.           Dbms_Output.put_line("records of the test_policy is "|| cnt);   
  7.           Insert into Test_Policy values(I_Policy_code, I_Policy_type);   
  8.           --commit;   
  9.       end P_Insert_Policy;   
  10.       PROCEDURE TEST_PL_SQL_ENTRY(I_POL_ID IN VARCHAR2, O_SUCC_FLG OUT VARCHAR2) AS   
  11.       strSql varchar2(500);   
  12.       cnt number := 0;   
  13.       BEGIN   
  14.          delete from test_policy;   
  15.          commit;   
  16.          insert into test_policy values("2010042101", "1");   
  17.          select count(1) into cnt from Test_Policy;   
  18.          Dbms_Output.put_line("records of the test_policy is "|| cnt);   
  19.             
  20.          P_Insert_Policy("2010042102", "2");   
  21.          rollback;   
  22.          commit;   
  23.          select count(1) into cnt from Test_Policy;   
  24.          Dbms_Output.put_line("records of the test_policy is "|| cnt);   
  25.          rollback;   
  26.             
  27.          select count(1) into cnt from Test_Policy;   
  28.          Dbms_Output.put_line("records of the test_policy is "|| cnt);   
  29.             
  30.     END TEST_PL_SQL_ENTRY;   
  31.     Run Pl/Sql=>   
  32.     结果是:   
  33.     records of the test_policy is 1 –-主事务中的操作已经commit   
  34.     records of the test_policy is 1 –-主事务的操作对Nested transaction有影响。   
  35.     records of the test_policy is 0 –-Nested transaction 的数据被主事务rollback.   
  36.     records of the test_policy is 0  
     
  • 1
  • 2
  • 3
  • 下一页
SQL TRACE和TKPROF的使用步骤Oracle内存结构研究-PGA篇相关资讯      Oracle数据库  Oracle入门教程  oracle数据库教程 
  • Oracle数据库全球化  (03月01日)
  • Oracle数据库日期过滤方法性能比较  (02/02/2015 13:20:26)
  • Oracle数据库安装中端口被占用问题  (10/29/2014 07:42:24)
  • 在CentOS 6.6上搭建C++运行环境并  (10/10/2015 19:44:40)
  • Oracle数据库无法使用localhost和  (11/14/2014 16:39:10)
  • 使用SQLT来构建Oracle测???用例  (08/28/2014 06:17:41)
本文评论 查看全部评论 (0)
表情: 姓名: 字数