gpt4 book ai didi

oracle - 触发器中的 pragma 自治事务

转载 作者:行者123 更新时间:2023-12-04 22:31:13 26 4
gpt4 key购买 nike

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

源表 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;

最佳答案

当父事务回滚时,将自治事务用于除日志记录之外的任何其他内容几乎肯定是错误的。这不是自治事务的一个很好的用途。

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

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

关于oracle - 触发器中的 pragma 自治事务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23774882/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com