[转帖]关于ORA-04091异常的出现原因,以及解决方案_MySQL, Oracle及数据库讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  MySQL, Oracle及数据库讨论区 »
总帖数
2
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 1901 | 回复: 1   主题: [转帖]关于ORA-04091异常的出现原因,以及解决方案        上一篇   下一篇 
Michaelkl
注册用户
等级:新兵
经验:66
发帖:3
精华:0
注册:2016-10-1
状态:离线
发送短消息息给Michaelkl 加好友    发送短消息息给Michaelkl 发消息
发表于: IP:您无权察看 2019-9-18 16:47:17 | [全部帖] [楼主帖] 楼主

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语句如下:

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语句如下:

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;




赞(0)    操作        顶端 
联动大白
注册用户
等级:列兵
经验:91
发帖:0
精华:0
注册:2015-5-27
状态:离线
发送短消息息给联动大白 加好友    发送短消息息给联动大白 发消息
发表于: IP:您无权察看 2019-11-16 0:30:00 | [全部帖] [楼主帖] 2  楼

为了方便大家阅读,我对文章中错误号来解释一下吧!

Error Id: ORA-06512

Title: at stringline string

Description:

at stringline string

Action:

Fix the problem causing the exception or write an exception handler for this condition. Or you may need to contact your application administrator or DBA.

Cause:

Backtrace message as the stack is unwound by unhandled exceptions.


Error Id: ORA-04088

Title: error during execution of trigger ’string.string’

Description:

error during execution of trigger ’string.string’

Action:

Check the triggers which were involved in the operation.

Cause:

A runtime error occurred during execution of a trigger.


Error Id: ORA-04091

Title: table string.string is mutating, trigger/function may not see it

Description:

table string.string is mutating, trigger/function may not see it

Action:

Rewrite the trigger (or function) so it does not read that table.

Cause:

A trigger (or a user defined plsql function that is referenced in this statement) attempted to look at (or modify) a table that was in the middle of being modified by the statement which fired it.


也许你已明白,但对一个人有用也是我存在的理由!^_^ By:持之以恒的大白

-- 来自: 北京联动北方科技有限公司



赞(0)    操作        顶端 
总帖数
2
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论