【问题标题】:pragma autonomous_transaction in a trigger触发器中的编译指示自治事务
【发布时间】:2014-07-09 14:31:46
【问题描述】:

我已经在一个表上编写了一个触发器,它会根据条件从另一个表中删除数据。 触发器具有编译指示自治事务,并且触发器按预期工作。但是,我确实想知道将来是否会出现任何问题,比如数据是否由多个用户/来源同时插入等等......有什么建议吗?

源表t1:

--------------------------------------------
| user_id | auth_name1 | auth_name2 | data |
--------------------------------------------
|  1      |  Name1     |  Name2      | d1  |
|  2      |  Name3     |  Name4      | d2  |
|  3      |  Name5     |  Name1      | d3  |
--------------------------------------------

目标表 t2:

   ------------------------------------------------
   | record_id |  identifier | status |   data1   |
   ------------------------------------------------
   |  100      |  Broken     |  11    |   Name1   |
   |  101      |  Reminder   |  99    |   Name1   |
   |  102      |  Broken     |  99    |   Name2   |
   |  103      |  Broken     |  11    |   Name4   |
   ------------------------------------------------

触发码:

create or replace trigger "ca"."t$t1"
    after update of auth_name1, auth_name2 on ca.t1  
    for each row
declare
    pragma autonomous_transaction;
begin
    if :new.auth_name1 is not null and :new.auth_name2 is not null then
         delete from ca.t2 ml
         where ml.identifier = 'Broken'
         and data1 = regexp_substr(:new.auth_name1, '\S+$')||' '||regexp_substr(:new.auth_name1, '^\S+')
         and status = 11;
         commit;
    end if;
end t$t1;

【问题讨论】:

    标签: oracle plsql triggers oracle11g plsqldeveloper


    【解决方案1】:

    在父事务回滚时将自治事务用于除日志记录之外的任何内容几乎可以肯定是错误的。这不是对自治事务的良好使用。

    例如,如果我更新 t1 中的一行但我的事务回滚,会发生什么情况。 t2 更改已经完成并提交,因此它们不会回滚。这通常意味着t2 数据现在不正确。事务的全部意义在于确保一组更改是原子的,并且要么完全成功,要么完全恢复。让代码部分成功几乎不是一个好主意。

    我很难看出使用自主交易在这里能给你带来什么。您经常会看到人们错误地使用自主事务来错误地解决变异触发错误。但是您发布的代码不会产生变异触发器错误,除非t2 上有一个行级触发器也试图更新t1 或一些引入变异表的类似机制。但是,如果是这种情况,使用自治事务通常会更糟,因为自治事务无法看到父事务中所做的更改,这几乎肯定会导致代码的行为与您期望的不同。

    【讨论】:

    • 感谢您的解释。我删除了编译指示自治事务;以及提交;声明
    猜你喜欢
    • 2017-01-10
    • 1970-01-01
    • 1970-01-01
    • 2011-11-20
    • 1970-01-01
    • 1970-01-01
    • 2022-01-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多