领先的中文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  作者:佚名   编辑:本站 点击:   [ 评论 ]

UES (998, 'Description for 998');

      -- Force invalid insert.
      INSERT INTO at_test (id, description)
      VALUES (999, NULL);
    EXCEPTION
      WHEN OTHERS THEN
        log_errors (p_error_message => SQLERRM);
        ROLLBACK;
    END;
    /

    PL/SQL procedure successfully completed.

    SELECT * FROM at_test WHERE id >= 998;

    no rows selected

    SELECT * FROM error_logs;

            ID LOG_TIMESTAMP
    ---------- ---------------------------------------------------------------------------
    ERROR_MESSAGE
    ----------------------------------------------------------------------------------------------------
             1 28-FEB-2006 11:10:10.107625
    ORA-01400: cannot insert NULL into ("TIM_HALL"."AT_TEST"."DESCRIPTION")


    1 row selected.

    SQL>

From this we can see that the LOG_ERRORS transaction was separate to the anonymous block. If it weren't, we would expect the first insert in the anonymous block to be preserved by the commit statement in the LOG_ERRORS procedure.
由此可知,LOG_ERRORS事务被分割为自治块。如果不是,我们可以期待在自治块插入的第一个数据被LOG_ERRORS存储过程的提交语句后保留。

Be careful how you use autonomous transactions. If they are used indiscriminately they can lead to deadlocks, and cause confusion when analyzing session trace. To hammer this point home, here's a quote from Tom Kyte posted on my blog (here):
小心你使用自治存储过程的方式。如果你胡乱使用,可能会引起死锁,同时在分析跟踪事务时引起冲突。下面是Tom Kyte在我的博客里提供的一些建议:

    ... in 999 times out of 1000, if you find yourself "forced" to use an autonomous transaction - it likely means you have a serious data integrity issue you haven't thought about.


    Where do people try to use them?

        * in that trigger that calls a procedure that commits (not an error logging routine). Ouch, that has to hurt when you rollback.
        * in that trigger that is getting the mutating table constraint. Ouch, that hurts *even more*

    Error logging - OK.

    Almost everything else - not OK.
1 2
如果图片或页面不能正常显示请点击这里
Oracle技术教程推荐文章

文章评论

BBS社区热贴