你好,游客 登录 注册 搜索
背景:
阅读新闻

ORA-04091和Compound Trigger(Oracle 11g)

[日期:2013-12-14] 来源:Linux社区  作者:chncaesar [字体:大 中 小]

Trigger created.

Now, there are many things wrong with this trigger. But the firstobvious clue that something was seriously wrong was their need to use anautonomous transaction. They did that because without it, an update wouldproduce the following result:

SQL< update currencies

  2    set primary_currency = 'Y';

 

update currencies

      *

ERROR at line 1:

ORA-04091: table OPS$TKYTE.CURRENCIES Is mutating, trigger/functionmay

not see it

ORA-06512: at "OPS$TKYTE.CURRENCIES_TRIGGER”, line 4

ORA-04088: error during execution of

trigger 'OPS$TKYTE.CURRENCIES_TRIGGER'

 

That is not really an error but more of a warning. Basically it issaying, “You are doing something so fundamentally wrong in your trigger thatOracle Database is just not going to permit you to do that.” If the database allowedthe trigger to read the table it was defined on, as the update was proceeding,the trigger would see the table partially updated. If five rows were beingupdated, the row trigger would see the table with one of the rows modified,then two, then three, and so on. It would see the table in a manner in whichthe table never should be seen.
By way of example, suppose the CURRENCIES table, above, was in placewith the sample data provided in the question and the trigger was permitted toread the table while it was changing. Now issue the following command:


update currencies

set primary_currency =

    decode(currency, 'USD','N', 'USN', 'Y')

where country = 'US'

and currency in ( 'USD', 'USN');


That should be OK, because it moves the primary currency flag fromUSD to USN. After the statement finishes, there will be only one primarycurrency row. But what if the rows get updated in order of first USN and thenUSD. Well, when the trigger fires the first time, it will see USN withPRIMARY_CURRENCY=‘Y’ and USD with PRIMARY_CURRENCY=‘Y’. The trigger will failthe statement, but the statement is supposed to succeed. On the other hand,what if the data is processed in the order of first USD and then USN? In thatcase, the trigger will fire and find zero PRIMARY_CURRENCY=‘Y’ rows and thenfire again, see only one, and be done.

So, for this trigger, the update will work for some data, sometimes.For other bits of data, the update will not work, sometimes. Two databases withthe same data will fail on different sets of rows and succeed on others. Itwill all depend on how the data happens to be organized on disk and in whatorder it is processed. And that is unacceptable (not to mention veryconfusing).


That, in a nutshell, is why the mutating table constraint exists: toprotect us from ourselves. But unfortunately, the developers asking thisquestion found a way around the mutating table constraint: the autonomoustransaction. That “feature” permits developers to query the table the triggeris firing on yet query it as if it were in another session/transactionaltogether. The trigger will not see its own modifications to the table, andthat is the huge flaw with this thinking: the trigger is attempting to validatethe very modifications it cannot see. The trigger’s sole purpose is to validatethe modifications to the data, but the trigger is reading the data before themodifications take place. It therefore cannot work!


Note that in Oracle Database 11g, there is a newfeature, the compound trigger that can be used to solve this issue.

linux
相关资讯       ORA-04091 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款