下表列出了以下PL/SQL组,所有锁定的囚室。 其他答复仅发现阻挡session,发现实际锁定的rows要求每行读和测试。
(尽管如此,你可能不需要执行这一法典。) 如果你重新出现僵局,通常会更容易使用<条码>GV$SESSION.BLOC_SESSION和其他有关数据词典。 请尝试另一种做法,然后由你来管理这一极其缓慢的法典。
首先,请提出一个样本表和一些数据。 页: 1
--Sample schema.
create table test_locking(a number);
insert into test_locking values(1);
insert into test_locking values(2);
commit;
update test_locking set a = a+1 where a = 1;
第2号会议设立了一个表格,以存放锁定的地雷监测器。
--Create table to hold locked ROWIDs.
create table locked_rowids(the_rowid rowid);
--Remove old rows if table is already created:
--delete from locked_rowids;
--commit;
在第2次会议上,PL/SQL组整读整个表格,每行各样,并储存锁定的ROWIDs。 警告说,这可能非常缓慢。 在你真实版本的这种询问中,将提及TEST_LOCines的内容改为你自己的表格。
--Save all locked ROWIDs from a table.
--WARNING: This PL/SQL block will be slow and will temporarily lock rows.
--You probably don t need this information - it s usually good enough to know
--what other sessions are locking a statement, which you can find in
--GV$SESSION.BLOCKING_SESSION.
declare
v_resource_busy exception;
pragma exception_init(v_resource_busy, -00054);
v_throwaway number;
type rowid_nt is table of rowid;
v_rowids rowid_nt := rowid_nt();
begin
--Loop through all the rows in the table.
for all_rows in
(
select rowid
from test_locking
) loop
--Try to look each row.
begin
select 1
into v_throwaway
from test_locking
where rowid = all_rows.rowid
for update nowait;
--If it doesn t lock, then record the ROWID.
exception when v_resource_busy then
v_rowids.extend;
v_rowids(v_rowids.count) := all_rows.rowid;
end;
rollback;
end loop;
--Display count:
dbms_output.put_line( Rows locked: ||v_rowids.count);
--Save all the ROWIDs.
--(Row-by-row because ROWID type is weird and doesn t work in types.)
for i in 1 .. v_rowids.count loop
insert into locked_rowids values(v_rowids(i));
end loop;
commit;
end;
/
最后,我们可以通过加入LOCKED_ROWIDS表来看待锁定的行。
--Display locked rows.
select *
from test_locking
where rowid in (select the_rowid from locked_rowids);
A
-
1