Oracle Autonomous Transactions

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:

  • 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.

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.

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.

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.

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)
VALUES (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_ERRORSprocedure.

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.

"... 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."

Oracle Autonomous Transactions(自治事务)的更多相关文章

  1. 关于Oracle AUTONOMOUS TRANSACTION(自治事务)的介绍

    AUTONOMOUS TRANSACTION(自治事务)的介绍 在基于低版本的ORACLE做一些项目的过程中,有时会遇到一些头疼的问题,比如想在执行当前一个由多个DML组成的transaction(事 ...

  2. (转)关于Oracle AUTONOMOUS TRANSACTION(自治事务)的介绍

    AUTONOMOUS TRANSACTION(自治事务)的介绍 在基于低版本的ORACLE做一些项目的过程中,有时会遇到一些头疼的问题,比如想在执行当前一个由多个DML组成的transaction(事 ...

  3. Oracle与SQL自治事务

    自治事务 自治事务是独立的事务操作,如果考虑到事务回滚,必须单独写成一个触发器来完成, 一个事务A在另一个事务B内被调用,那个事务A是自治事务,自治事务A执行过程中会脱离其session内未执行完毕的 ...

  4. ORACLE PRAGMA AUTONOMOUS_TRANSACTION 自治事务 单独提交某一段操作

    个人使用示例: CREATE OR REPLACE PROCEDURE logs(p_remark VARCHAR2, p_log CLOB) AS PRAGMA AUTONOMOUS_TRANSAC ...

  5. ORACLE触发器的自治事务的注意事项

    直接上代码: Create OR replace Trigger TR_ROBXMX_CLDJBHHX After INSERT OR UPDATE OR DELETE ON ROBXMX1 --要监 ...

  6. Oracle - 自治事务autonomous transaction

    自治事务 - autonomous transaction 在Oracle数据库中,有时候我们会希望记录一个过程或者函数的运行日志,不管正常运行结束还是触发异常结束,都要记录. 正常结束的没有问题,但 ...

  7. [转]了解oracle自治事务

    http://blog.csdn.net/indexman/article/details/7799862 1.什么是Oracle自治事务 在官方文档中,是这样的定义的“Autonomous tran ...

  8. Oracle自治事务

    定        义: Autonomous transactions are independent transactions that can be called from within anot ...

  9. Oracle EBS 自治事务

    自治事务程序主要是自主性,那就是,独立于主要的事务.之所以独立,或者提交之后会影响其他事务处理,本质在于它本身符合编译指令的规则,也就是说它属于在编译阶段就执行的指令,而不是在运行阶段执行的. 当自治 ...

随机推荐

  1. Net::OpenSSH 使用例子

    [root@dr-mysql01 mojo]# cat a1.pl use Net::OpenSSH; my $host = '121.4xx.xx1.41'; my $user = 'root'; ...

  2. visual c++ 2010安装未成功

    可能是已经安装了其他版本的Microsoft visual studio 参考: http://answers.microsoft.com/zh-hans/windows/forum/windows_ ...

  3. JavaScript的实现

    了解了JavaScript是干什么的< 对一些词的理解 >,下面该知道它是怎么实现的. 一个完整的JavaScript是由三部分组成的,如下图 ECMAScript 可以为不同种类的宿主环 ...

  4. 插件化—配置xml的辅助测试

    1.xml文件,xml文件在res/xml目录下 <?xml version="1.0" encoding="utf-8"?> <infos& ...

  5. ELK 之一:ElasticSearch 基础和集群搭建

    一:需求及基础: 场景: 1.开发人员不能登录线上服务器查看详细日志 2.各个系统都有日志,日志数据分散难以查找 3.日志数据量大,查询速度慢,或者数据不够实时 4.一个调用会涉及到多个系统,难以在这 ...

  6. Qt核心剖析: 寻找 QObject 的源代码

    本来打算把<Qt学习之路>作为一个类似教程的东西,所以就不打算把一些关系到源代码的内容放在那个系列之中啦.因此今天就先来看一个新的开始吧!这个系列估计不会进展很快,因为最近公司里面要做 f ...

  7. GDOI2015——已成梦

    今年GDOI(2015)在韶关北江中学(没记错的话应该是武江区)举行,感觉这五天就是一场梦,一场包含苦辣的梦. Day0 坐了一个上午的车,而且车内的空气又不好,感觉整个人都累倒下了. 到了北江之后吃 ...

  8. uoj Goodbye Jiawu

    这次比赛真是太伤我心了. 比(惨)赛(不)结(忍)果(睹) 完挂感言 uoj round 5已经挂了一次了,没想到还要再挂第二次. 这次比赛的期望得分是\(100+100+100+70+10\)的.没 ...

  9. Ubuntu下ssh免password登录安装

    1.首先在本机安装openssh-server和openssh-client. 命令:sudo apt-get install openssh-server openssh-client 2.在检查当 ...

  10. Demo XML 、 JSON 解析 AND 网络HTTP请求

    有道云笔记分享:http://note.youdao.com/share/?id=7950b949a5017a698a9ecc95bc250ec5&type=note 后台服务端:C#.服务器 ...