领先的中文IT技术网站    IT技术从现在起飞

飞诺旗下: 技术社区 | 在线电子书 | 在线试题 | 资源下载 | 飞诺搜索 | 技术博客
用户名: 密   码:
   飞诺网 加入收藏
飞诺网 Oracle 新闻频道 开发频道 系统频道 服务器 网络频道 网络安全 Java频道 C/C++ PHP开发 电子书 资源下载 社 区 博 客 在线试题
数据库 Access Mysql Mssql Oracle Sybase FoxPro db2 数据库相关文章
编程开发 JAVA C/C++ C++ VC C语言 VB C# Delphi Foxpro 汇编 shell编程 游戏开发 软件工程师 WEB开发 PHP ASP Asp.net JSP AJAX CGI JavaScript HTML CSS 数据库 MSSQL Mysql Oracle Access Sybase DB2 sql2005 Office Word Excel Powerpoint Wps 认证考试 二级C语言 三级网络 程序员 网络工程师 思科认证

您当前的位置:飞诺网 >>  数据库 >>  Oracle >> Oracle技术教程

Oracle自治事务的介绍(Autonomous Transactions)PRAGMA AUTONOMOUS_TRANSACTION;

www.firnow.com    时间 : 2009-01-09  作者:佚名   编辑:本站 点击:   [ 评论 ]

Autonomous transactions allow you to leave the context of the calling transaction, perform an independant transaction, and return to the calling transaction without affecting it's state. The autonomous transaction has no link to the calling transaction, so only commited data can be shared by both transactions.
自治事务允许你离开调用的事务上下文,执行一个独立的事务,然后返回调用的事务而不会影响到调用事务的状态。自治事务和调用事务不同,只有提交的事务才会在事务见共享。


The following types of PL/SQL blocks can be defined as autonomous transactions:
以下的PL/SQL代码块可以定义为自治事务。

    * Stored procedures and functions. 存储过程和函数
    * Local procedures and functions defined in a PL/SQL declaration block. 定义在声明块里的本地存储过程和函数
    * Packaged procedures and functions. 打包的存储过程和函数
    * Type methods. 类型方法
    * Top-level anonymous blocks. 顶层的匿名块

The easiest way to understand autonomous transactions is to see them in action. To do this, we create a test table and populate it with two rows. Notice that the data is not commited.
最简单的理解自治事务的方法是查看他们的行为。我们创建一个测试表格,然后放入2行数据,注意数据没有提交。


    CREATE TABLE at_test (
      id NUMBER NOT NULL,
      description VARCHAR2(50) NOT NULL
    );

    INSERT INTO at_test (id, description) VALUES (1, 'Description for 1');
    INSERT INTO at_test (id, description) VALUES (2, 'Description for 2');

    SELECT * FROM at_test;

            ID DESCRIPTION
    ---------- --------------------------------------------------
             1 Description for 1
             2 Description for 2

    2 rows selected.

    SQL>

Next, we insert another 8 rows using an anonymous block declared as an autonomous transaction, which contains a commit statement.
下一步,我们使用匿名的自治事务块插入另外8行数据,同时提交。

    DECLARE
      PRAGMA AUTONOMOUS_TRANSACTION;
    BEGIN
      FOR i IN 3 .. 10 LOOP
        INSERT INTO at_test (id, description)
        VALUES (i, 'Description for ' || i);
      END LOOP;
      COMMIT;
    END;
    /

    PL/SQL procedure successfully completed.

    SELECT * FROM at_test;

            ID DESCRIPTION
    ---------- --------------------------------------------------
             1 Description for 1
             2 Description for 2
             3 Description for 3
             4 Description for 4
             5 Description for 5
             6 Description for 6
             7 Description for 7
             8 Description for 8
             9 Description for 9
            10 Description for 10

    10 rows selected.

    SQL>

As expected, we now have 10 rows in the table. If we now issue a rollback statement we get the following result.
和预想的一样,我们得到了10行数据。如果我们执行一个回滚(rollback)语句,我们得到了如下的结果

    ROLLBACK;
    SELECT * FROM at_test;

            ID DESCRIPTION
    ---------- --------------------------------------------------
             3 Description for 3
             4 Description for 4
             5 Description for 5
             6 Description for 6
             7 Description for 7
             8 Description for 8
             9 Description for 9
            10 Description for 10

    8 rows selected.

    SQL>

The 2 rows inserted by our current session (transaction) have been rolled back, while the rows inserted by the autonomous transactions remain. The presence of the PRAGMA AUTONOMOUS_TRANSACTION compiler directive made the anonymous block run in its own transaction, so the internal commit statement did not affect the calling session. As a result rollback was still able to affect the DML issued by the current statement.
被我们当前事务插入的2行数据被回滚了,而被自治事务插入的数据继续存在。编译描述符 PRAGMA AUTONOMOUS_TRANSACTION 使得自治块在自己的事务里运行,所以内部的提交语句不会影响调用方的事务。

Autonomous transactions are commonly used by error logging routines, where the error messages must be preserved, regardless of the the commit/rollback status of the transaction. For example, the following table holds basic error messages.
自治事务一般用于日常的错误日志,错误信息必须保留,无论事务是提交还是回滚。例如下面的表保存了基本的错误信息。

    CREATE TABLE error_logs (
      id NUMBER(10) NOT NULL,
      log_timestamp TIMESTAMP NOT NULL,
      error_message VARCHAR2(4000),
      CONSTRAINT error_logs_pk PRIMARY KEY (id)
    );

    CREATE SEQUENCE error_logs_seq;

We define a procedure to log error messages as an autonomous transaction.
我们定义了一个自治事务的存储过程来记录错误信息

    CREATE OR REPLACE PROCEDURE log_errors (p_error_message IN VARCHAR2) AS
      PRAGMA AUTONOMOUS_TRANSACTION;
    BEGIN
      INSERT INTO error_logs (id, log_timestamp, error_message)
      VALUES (error_logs_seq.NEXTVAL, SYSTIMESTAMP, p_error_message);
      COMMIT;
    END;
    /

The following code forces an error, which is trapped and logged.
下面的代码强制产生一个错误,被捕获且记录到日志

    BEGIN
      INSERT INTO at_test (id, description)
      VAL
 1 2
如果图片或页面不能正常显示请点击这里
Oracle技术教程推荐文章

文章评论

BBS社区热贴