ORA-04091: table is mutating, trigger/function may not see it
今天在论坛里发现了一个关于ORA-04091的老帖子,收获良多,特此整理一下
关于ORA-04091: table is mutating, trigger/function may not see it的分析
当DML操作触发trigger的时候,如果trigger的程序块中需要对当前表进行修改或查询的时候,就会报错
ORA-04091: table is mutating, trigger/function may not see it
这是有在被触发TRIGGER工作的时候,默认把当前表表锁死,不允许对其进行操作,所以trigger包含对当前表的DML操作就会报错,那怎么办?最常用的方法是通过修改SQL避免错误.
- create or replace trigger tr_test
- after insert
- on test
- for each row
- begin
- update test set column2=123 where column1=:new.column1
- end tr_test;
这就是个典型的错误的例子,肯定会报错ORA-04091,这个trigger是为了修改新插入的行的某列,因为插入后当前表已经被锁死了,所以根本没有办法update,所以报错。
那应该怎么改呢?
- create or replace trigger tr_test
- before insert
- on test
- for each row
- begin
- :new.column2:=123
- end tr_test;
在出入前就修改好要修改的值,就不会报错了
但是这种方法浪费时间精力,更重要的并不是所有问题都可以找到这样的方法绕过去.
还有一种方法是加 PRAGMA AUTONOMOUS_TRANSACTION;
- create or replace trigger tr_test
- after insert
- on test
- for each row
- declare
- PRAGMA AUTONOMOUS_TRANSACTION;
- begin
- update test set column2=123 where column1=:new.column1 ;
- commit;
- end tr_test;

这样也可以执行成功。
AUTONOMOUS_TRANSACTION是指在function,procedure,trigger等subprograms中对事务进行自治管理,当在别的pl/sql block里取调用这些subprograms的时候这些subprograms并不随着父pl/sql block的失败而回滚,而是自己管自己commit;
注意慎用AUTONOMOUS_TRANSACTION。一个DML可能触发很多次触发器,因此产生了大量独立的事务,很容易产生死锁。
ASKTOM上对AUTONOMOUS_TRANSACTION的看法是:唯一的用途就是作审计日志,其他一概不该使用。
有人建议是取消使用触发器,把你的业务逻辑写到存储过程去。
ORA-04091: table is mutating, trigger/function may not see it的更多相关文章
- 错误"ORA-04091: table is mutating, trigger/function may not see it"的原因以及解决办法
错误的原因该错误是在编写trigger时常遇到的问题,其根本原因是由于对本表的操作造成的.对于使用了for each row 的触发器,做了DML操作(delete,update,insert),还没 ...
- ORA-04091: table xxxx is mutating, trigger/function may not see it
今天同事让我看一个触发器为什么老是报错,当执行DML语句触发触发器后,会报ORA-04091错误:ORA-04091: table xxxx is mutating, trigger/function ...
- ORA-04091: table xxx is mutating, trigger/function may not see it
Connected to Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 Connected as tbcs SQL> SQL ...
- Can't update table 'test_trigger' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
[Err] 1442 - Can't update table 'test_trigger' in stored function/trigger because it is already used ...
- MySQL触发器更新本表数据异常:Can't update table 'tbl' in stored function/trigger because it
MySQL触发器更新本表数据异常:Can't update table 'tbl' in stored function/trigger because it 博客分类: 数据库 MySQLJava ...
- Can’t update table ‘xxx’ in stored function/trigger because it is already used by statement which invoked this stored function/trigger
MySQL: Solution for ERROR 1442 (HY000): Can't update table 'xxx' in stored function/trigger because ...
- 【Azure 应用服务】部署Kafka Trigger Function到Azure Function服务中,解决自定义域名解析难题
问题描述 经过前两篇文章,分别使用VM搭建了Kafka服务,创建了Azure Function项目,并且都在本地运行成功. [Azure Developer]在Azure VM (Windows) 中 ...
- MySQL - 问题集 - 触发器更新本表数据异常"Can’t update table ‘tbl’ in stored function/trigger because it is already used by statement which invoked this"
如果你在触发器里面对刚刚插入的数据进行了 insert/update, 则出现这个问题.因为会造成循环的调用. create trigger test before update on test fo ...
- PostgreSQL trigger (function) examples
postgres=# \c warehouse_db You are now connected to database "warehouse_db" as user " ...
随机推荐
- Java Class.cast方法
1.Java api public T cast(Object obj); Casts an object to the class or interface represented 解释的比较笼统, ...
- jquery.dataTable.js 基础配置
$(document).ready(function () { $('#dataTables-example').DataTable({ responsive: true, "bPagina ...
- iOS UIPageViewController
UIPageViewController是App中常用的控制器.它提供了一种分页效果来显示其childController的View.用户可以通过手势像翻书一样切换页面.切换页面时看起来是连续的,但静 ...
- 【代码笔记】iOS-文字走马灯效果
一,效果图. 二,工程图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> @interface RootViewController ...
- 【代码笔记】iOS-圆角矩形
代码: - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. se ...
- 基于Ruby的watir-webdriver自动化测试方案与实施(四)
接着基于Ruby的watir-webdriver自动化测试方案与实施(三) http://www.cnblogs.com/Javame/p/4159468.html 继续 ... ... 首先回忆 ...
- AngularJs中,如何在render完成之后,执行Js脚本
AngularJs是Google开源的前端JS框架.使用AngularJs, 我们能够容易地.健壮的开发出类似于Gmail一样的单页Web应用.AngularJs这个新兴的MVC前端框架,具有以下特点 ...
- XtraBackup出现 Can't connect to local MySQL server through socket '/tmp/mysql.sock'
Xtrabackup做备份时遇到下面错误信息MySQL server: Can't connect to local MySQL server through socket '/tmp/mysql.s ...
- GConf error:Failed to contact configuration server
Linux系统运行一直正常,但是图形界面使用root账号登录时遇到下面错误,第一次遇到这么怪异的状况 具体错误信息如下所示: GConf error:Failed to contact configu ...
- [20141124]sql server密码过期,通过SSMS修改策略报错
背景: 新建了用户,没有取消掉强制密码策略 修改掉策略报错 错误: The CHECK_POLICY and CHECK_EXPIRATION options cannot be turned OFF ...