gpt4 book ai didi

oracle - 是否可以锁定触发器;如何确定它是?

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

在回答 Will I miss any changes if I replace an oracle trigger while my application is running? ,我去看看触发器是否被 INSERT 语句锁定。不是,我在互联网上找不到任何建议可以锁定触发器的内容。

如果我在一个 session 中运行以下内容:

create table test_trigger (id number);
create table test_trigger_h (id number);

create or replace trigger test_trigger_t
after insert on test_trigger for each row
begin
insert into test_trigger_h (id) values (:new.id);
end;
/

insert into test_trigger
select level
from dual
connect by level <= 1000000;

然后在第二个 session 中尝试找出发生了什么锁,我得到以下信息:
select object_name, object_type
, case l.block
when 0 then 'Not Blocking'
when 1 then 'Blocking'
when 2 then 'Global'
end as status
, case v.locked_mode
when 0 then 'None'
when 1 then 'Null'
when 2 then 'Row-S (SS)'
when 3 then 'Row-X (SX)'
when 4 then 'Share'
when 5 then 'S/Row-X (SSX)'
when 6 then 'Exclusive'
else to_char(lmode)
end as mode_held
from v$locked_object v
join dba_objects d
on v.object_id = d.object_id
join v$lock l
on v.object_id = l.id1
join v$session s
on v.session_id = s.sid
;

OBJECT_NAME OBJECT_TYPE STATUS MODE_HELD
-------------------- -------------------- --------------- ---------------
TEST_TRIGGER TABLE Not Blocking Row-X (SX)
TEST_TRIGGER_H TABLE Not Blocking Row-X (SX)

根据 Oracle 的说法,触发器没有被锁定。

但是,如果我在 INSERT 语句运行时尝试替换触发器,则直到语句完成(不包括提交)后才会替换触发器,这意味着触发器已锁定。

在这种情况下,触发器是否被锁定,如果是,如何确定它是?

最佳答案

要确定触发器(以及任何其他存储过程)是否被锁定,V$ACCESS可以查询动态性能 View 。

Session #1

insert into test_trigger
select level
from dual
connect by level <= 1000000;

Session #2

SQL> select *
2 from v$access
3 where object = upper('test_trigger_t')
4 ;


Sid Owner Object Type Con_Id
--------------------------------------
441 HR TEST_TRIGGER_T TRIGGER 3

那些类型的锁是库缓存引脚(库缓存锁是资源( TM 类型的锁)锁),需要确保在 session 执行时保护对象不被修改。
--session sid # 441
insert into test_trigger
select level
from dual
connect by level <= 1000000;


-- session sid #24
create or replace trigger test_trigger_t
after insert on test_trigger for each row
begin
insert into test_trigger_h (id) values (:new.id);
end;

-- Session # 3
select vs.sid
, vs.username
, vw.event
from v$session vs
join v$session_wait vw
on (vw.sid = vs.sid)
join v$access va
on (va.owner = vs.username)
where vs.username = 'HR'

结果:
Sid Username Event 
--------------------------
24 HR library cache pin
....
441 HR log file switch (checkpoint incomplete)

在这里我们可以看到 session #441 等待日志文件切换, session #24 等待库缓存 pin。

关于oracle - 是否可以锁定触发器;如何确定它是?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18460260/

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