技术开发 频道

Oracle:嵌套事务与自治事务的思考

  自治事务(Autonomous transaction)

  1、下面是来自于Oracle上对自治事务的描述

autonomous transactions does not depend on the main transaction. For example, if the main transaction rolls back, nested transactions roll back, but autonomous transactions do not.

  autonomous transactions‘ committed changes are visible to other transactions immediately. (A nested transaction
's committed changes are not visible to other transactions until the main transaction commits.)

   自治事务(以下简称AT)是由主事务(以下简称MT)调用但是独立于MT的事务。在自治事务被调用执行时,MT被挂起,在自治事务内部,一系列的DML可以被执行并且commit或rollback. 自治事务防止嵌套提交,使事务在自己的事务区内提交或回滚不会影响其他的事务。由于自治事务的独立性,它的commit和rollback并不影响MT的执行效果。在自治事务执行结束后,主事务获得控制权,又可以继续执行了。

  实现自治事务的定义,只需下列PL/SQL的声明部分加上PRAGMA AUTONOMOUS_TRANSACTION 就可以了。

   优异的匿名PL/SQL块

   Functions 或 Procedure.

  2、 定义一个自治事务:

1.Procedure p_insert_policy_new(i_policy_code Varchar2(20),  
2.                                i_policy_type char(1)) as  
3.  Pragma Autonomous_Transaction;--define auto trans  
4.  cnt number := 0;  
5.  begin  
6.      select count(1) into cnt from test_policy;  
7.      Dbms_Output.put_line('records of the test policy table is: '||cnt);      
8.        
9.      Insert into Test_Policy values(I_Policy_code, I_Policy_type);                            
10.      commit;  
11.      select count(1) into cnt from test_policy;  
12.      Dbms_Output.put_line('records of the test policy table is: '||cnt);    
13.  end p_insert_policy_new;  
14.--call auto trans procedure  
15.PROCEDURE TEST_PL_SQL_ENTRY(  
16.                              I_POL_ID IN VARCHAR2,  
17.                              O_SUCC_FLG OUT VARCHAR2) AS  
18.  strSql varchar2(500);  
19.  cnt number := 0;  
20.  v_policyCode t_contract_master.policy_code%type;  
21.  BEGIN  
22.     delete from test_policy;  
23.     commit;  
24.     insert into test_policy values('2010042101', '1');  
25.     select count(1) into cnt from Test_Policy;  
26.     Dbms_Output.put_line('records of the test_policy is '|| cnt);  
27.        
28.     p_insert_policy_new('2010042102', '2');  
29.     select count(1) into cnt from Test_Policy;  
30.     Dbms_Output.put_line('records of the test_policy is '|| cnt);  
31.     rollback;  
32.     select policy_code into v_policyCode from test_policy;  
33.     Dbms_Output.put_line('policy_code: '|| v_policyCode);  
34.     commit;  
35.     select count(1) into cnt from Test_Policy;  
36.     Dbms_Output.put_line('records of the test_policy is '|| cnt);  
37.     rollback;  
38.        
39.     select count(1) into cnt from Test_Policy;  
40.     Dbms_Output.put_line('records of the test_policy is '|| cnt);  
41.        
42.  END TEST_PL_SQL_ENTRY;  
43.Run pl/sql=>  
44.records of the test_policy is 1-Master trans has been committed.  
45.records of the test policy table is: 0 -–Auto trans isn’t affected by master trans.  
46.records of the test policy table is: 1-Auto trans has been committed.  
47.records of the test_policy is 2  
48.policy_code: 2010042102-rollback affected master trans  
49.records of the test_policy is 1    
50.records of the test_policy is 1  

  3、总结Auto Transaction:

  其中 pragma 关键字的作用是通知 PL/SQL 编译器,将声明它的这个 PL/SQL 代码块分割为一个自治的或独立的事务。定义自治事务时,要遵守以下几条规则:

  ① 如果 PL/SQL 块是匿名的,那么该匿名 PL/SQL 块必须是一个顶层块。

  ② 如果 PL/SQL 块不是匿名的,那么它必须作为包或存储过程序单元一部分的一个过程或函数。当在包中定义自治事务时,只有包中具体的函数或过程才能被指定为自治的的。

  ③ PL/SQL 块也可以是存储对象类型的一个方法

  ④PL/SQL 块也可以是一个数据库触发器

  自治事务的一些关键问题

  ①带 ALTER SESSION 的自治事务

  自治事务应该与主事务共享一个会话,因此通过 ALTER SESSION 语句,对该会话的任何修改,对自治事务和主事务应该都是可见的。但自治事务执行与主事务不同的上下文中。任何从自治块中引发的自治子程序调用都与自治事务共享相同的事务上下文。

  ②自治事务与死锁

  必须以下面的这种方式定义自治事务:

  死锁在自治事务试图访问一个被主事务占用的资源时发生,这时主事务会挂起,直到自治事务结束。自治事务死等主事务释放资源,结果可能导致死锁。

  ③ 定义自治事务的条件

  只有顶层匿名块才能包括 PRAGMA AUTONOMOUS_TRANSTRACTION

  ④ COMMIT 或 ROLLBACK 行为与自治事务

  自治事务是以 commit 或 rollback 语句结束的。因此,应该在自治事务程序的内部显式地包含一个 commit 或 rollback 语句。如果没有这样做,任何一个未确定的事务都会回滚。这是一个事务级的回滚,而不是一个语句级的回滚。

  ⑤ 异常与自治事务

  当自治事务以一个异常的方式退出时,就发生一个事务级的回滚,自治事务中所有未确定的改变都会回滚 .

  ⑥ 多个自治事务

  一个自治事务的上下文中,初始化多个自治事务。可以在一个自治块内定义多个 commit 或 rollback 语句来完成这项任务,当包含一个 rollback 时,它就会属于当前事务,而不属于主事务。

  ⑦ 自治事务的并发问题

  自治事务与主事务是并发运行的,初始化文件 init.ora 中的 transactions 参数决定着每个会话中并发事务的数量。

  ⑧通过自治事务和从 SQL 中调用用户自定义函数

  通过自治事务,可以从 SQL 中调用用户自定义函数来执行 DML 操作。只需把用户自定义函数定义为自治事务,函数就变为可从 SQL 中调用的。

  ⑨自治事务和隔离等级

  应该在自治事务中给出 commit 或 rollback 命令,一旦 commit 或 rollback 在自治事务内部执行,那些改变对主事务而言是可见。但是 oracle 也允许从主事务中通过设定主事务的隔离等级为 SERIALIZABLE ,而不是默认的 READ COMMITTED ,对主事务隐藏这些改变,这可以通过 Set TRANSCTION 语句完成,其语法如下:

Set transaction isolation level serializable;

   关于隔离等级的问题,以下两点值得注意:

  当自治事务以 commit 或 rollback 而结束时,由自治事务造成的改变对主事务以外的其它事务是可见的 .

  设置隔离等级为 serializable ,对主事务隐藏自治事务的变化。直到主事务提交,主事务一旦提交,自治事务中的改变对自治事务也就可见。

  ⑩ 隔离等级

  隔离等级是一种处理修改数据库事务的方法,它影响到一个事务中的改变对另一个事务的可见性。 SQL92 中定义了 4 种隔离级别即:

1.READ UNCOMMITTED    
2. REPEATABLE READ    
3. READ COMMOTTED    
4. SERIALIZABLE  

         oracle 支持 READ COMMOTTED 和 SERIALIZABLE 两种隔离级别

  Read committed :这种设置是 oracle 默认的 , 它使 oracle 查询可以看到查询之前提将近的数据。换句话说,这种模式的事务是基于每句一致的事务集的。

  Serializable : 这种设置意味着一个事务中的所有语句,都在操作一个事务开始时的数据库映像中,这意味着没有 commit 之前,当前事务都不能看到其它事务所提交的数据。

0
相关文章