关于ORA-04091异常的出现原因分析及解决方案
作者:悄悄小土豆 时间:2024-01-22 22:29:20
1、异常出现的场景.
:在使用Hibernate做为项目持久层的情况下,需要对某一张表进行一个扩展,扩展操作便是在该表上创建一个触发器。将表中的数据读入到其他表中。
-4091,ORA-04091: 表 DD123.DO_TABLE_47 发生了变化, 触发器/函数不能读它
SQL语句如下:
Sql代码
drop table tr_table;
create table tr_table( --触发器作用表
tab_id number primary key,
tab_name varchar2(30) NOT NULL
)
create table ts_table as select * from tr_table; --提供扩展功能的表
--定义的触发器,在tr_table表插入和更新数据之后向ts_table表插入当前操作的信息行。
create trigger iu_table
after insert or update on tr_table
for each row
begin
insert into ts_table select * from tr_table t where t.tab_id = :new.tab_id;
end is_table;
--对tr_table执行插入操作,触发ts_table插入操作
insert into tr_table(tab_id,tab_name) values(1,'test');
--弹出错误信息提示
--ORA-04091:表tr_table发生了变化 触发器/函数不能读它
--ORA-06512: 在iu_table line 2
--ORA-04088: 触发器iu_table 执行过程中出错
2、问题分析
:在Oracle中执行DML语句的时候是需要显示进行提交操作的。当我们进行插入的时候,会触发触发器执行对触发器作用表和扩展表的种种操作,但是这个时候触发器和插入语句是在同一个事务管理中的,因此在插入语句没有被提交的情况下,我们无法对触发器作用表进行其他额外的操作。如果执行其他额外的操作则会抛出如上异常信息。
3、解决方案
:1,我们知道,出错的原因是因为触发器和DML语句在同一事务管理中,所以方案一便是将触发器和DML语句分成两个单独的事务处理。这里可以使用Pragma autonomous_transaction; 告诉Oracle触发器是自定义事务处理。
SQL语句如下:
Sql代码
create trigger iu_table
after insert or update on tr_table
for each row
declare --这里是关键的地方,在变量申明的地方,指定自定义事务处理。
pragma autonomous_transaction;
begin
insert into ts_table select * from tr_table t where t.tab_id = :new.tab_id;
--这里需要显示提交事务
commit;
end iu_table;
create trigger iu_table
after insert or update on tr_table
for each row
declare --这里是关键的地方,在变量申明的地方,指定自定义事务处理。
pragma autonomous_transaction;
begin
insert into ts_table select * from tr_table t where t.tab_id = :new.tab_id;
--这里需要显示提交事务
commit;
end iu_table;
:2,在Oracle Trigger中有:new,:old两个特殊变量,当触发器为行级触发器的时候,触发器就会提供new和old两个保存临时行数据的特殊变量,我们可以从俩个特殊的变量中取出数据执行扩张表的DML操作。
SQL语句如下:
create trigger iu_table
after insert on tr_table
for each row
begin
insert into ts_table(tab_id,tab_name) values(:new.tab_id,:new.tab_name);
--这里需要注意,要知道不同的触发类型其特殊变量:new和:old保存的值的区别。
--commit; 注意使用方案二,这里不能显示的进行提交操作操作,trigger中在没有声明自定义事务管理的时候,不能执行显示提交。
end iu_table;
来源:https://blog.csdn.net/u012069924/article/details/18450391
标签:ORA,04091,异常
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
数据库基本操作语法归纳总结
2024-01-19 04:15:40
Python打印不合法的文件名
2021-06-29 03:40:19
浅谈Python如何获取excel数据
2021-04-06 09:30:32
![](https://img.aspxhome.com/file/2023/6/120296_0s.png)
python 多维切片之冒号和三个点的用法介绍
2023-05-05 11:57:23
![](https://img.aspxhome.com/file/2023/4/124934_0s.jpg)
MySQL常见错误有哪些_MySQL常见错误的快速解决方法
2024-01-25 09:16:31
![](https://img.aspxhome.com/file/2023/0/83370_0s.png)
goland -sync/atomic原子操作小结
2024-04-26 17:20:08
Python Flask异步发送邮件实现方法解析
2023-08-15 04:29:12
vscode配置远程开发与免密登录的技巧
2023-04-29 02:43:41
![](https://img.aspxhome.com/file/2023/3/132403_0s.png)
Python使用socket模块实现简单tcp通信
2021-04-20 14:54:14
Pycharm Available Package无法显示/安装包的问题Error Loading Package List解决
2022-09-09 18:33:23
![](https://img.aspxhome.com/file/2023/7/135147_0s.png)
Python sklearn CountVectorizer使用详解
2023-06-20 08:19:05
![](https://img.aspxhome.com/file/2023/4/76024_0s.png)
Fibonacci数,Θ(log n)
2010-03-28 13:28:00
Python 相对路径和绝对路径及写法演示
2023-01-17 15:23:07
python爬虫中抓取指数的实例讲解
2022-02-18 04:59:58
![](https://img.aspxhome.com/file/2023/4/101564_0s.png)
python向字符串中添加元素的实例方法
2023-08-24 00:59:21
![](https://img.aspxhome.com/file/2023/4/97284_0s.jpg)
Pygame游戏开发之太空射击实战敌人精灵篇
2023-09-15 20:03:52
![](https://img.aspxhome.com/file/2023/5/66345_0s.png)
win10从零安装配置pytorch全过程图文详解
2022-07-01 20:54:55
![](https://img.aspxhome.com/file/2023/1/105401_0s.png)
说说CSS的优先权 考虑CSS的继承与层叠
2008-12-11 13:33:00
vue项目中如何调用多个不同的ip接口
2024-04-10 10:32:37
GO语言入门Golang进入HelloWorld
2024-05-09 09:32:12
![](https://img.aspxhome.com/file/2023/7/127427_0s.gif)