您好,登錄后才能下訂單哦!
這篇文章主要講解了“數(shù)據(jù)庫中各種帶鎖游標(biāo)加鎖的時(shí)機(jī)分析”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“數(shù)據(jù)庫中各種帶鎖游標(biāo)加鎖的時(shí)機(jī)分析”吧!
我建立了一個(gè)表并生成一行數(shù)據(jù):
create table plch_one_row (id number); insert into plch_one_row values (1); commit;
然后我建立一個(gè)過程來檢查我的表里這行數(shù)據(jù)是否被鎖住。我用的方法是在一個(gè)帶有自治事務(wù)的過程里試圖對這行進(jìn)行加鎖。
CREATE OR REPLACE PROCEDURE plch_check_lock AS PRAGMA AUTONOMOUS_TRANSACTION; resource_busy EXCEPTION; PRAGMA EXCEPTION_INIT (resource_busy, -54); l_id plch_one_row.id%TYPE; BEGIN SELECT id INTO l_id FROM plch_one_row FOR UPDATE NOWAIT; DBMS_OUTPUT.put_line ('Not locked'); COMMIT; EXCEPTION WHEN resource_busy THEN DBMS_OUTPUT.put_line ('Locked'); END; /
下列的選項(xiàng)中,哪些可以用來代替下面這個(gè)塊中的/* code */注釋,從而執(zhí)行之后會(huì)顯示"Not locked"? 你可以假定在執(zhí)行之前表上沒有鎖。
BEGIN /* code */ plch_check_lock; END; /
(A)
begin for rec in (select 1/0 from plch_one_row for update) loop null; end loop; exception when zero_divide then null; end;
SQL> BEGIN 2 begin 3 for rec in (select 1 / 0 from plch_one_row for update) loop 4 null; 5 end loop; 6 exception 7 when zero_divide then 8 null; 9 end; 10 plch_check_lock; 11 END; 12 / Not locked PL/SQL procedure successfully completed SQL>
(B)
declare cursor cur is select 1/0 from plch_one_row for update; begin for rec in cur loop null; end loop; exception when zero_divide then null; end;
SQL> BEGIN 2 declare 3 cursor cur is 4 select 1 / 0 from plch_one_row for update; 5 begin 6 for rec in cur loop 7 null; 8 end loop; 9 exception 10 when zero_divide then 11 null; 12 end; 13 plch_check_lock; 14 END; 15 / Locked PL/SQL procedure successfully completed SQL>
(C)
declare cursor cur is select 1/0 from plch_one_row for update; begin savepoint before_loop; for rec in cur loop null; end loop; exception when zero_divide then rollback to before_loop; end;
SQL> BEGIN 2 declare 3 cursor cur is 4 select 1 / 0 from plch_one_row for update; 5 begin 6 savepoint before_loop; 7 for rec in cur loop 8 null; 9 end loop; 10 exception 11 when zero_divide then 12 rollback to before_loop; 13 end; 14 plch_check_lock; 15 END; 16 / Not locked PL/SQL procedure successfully completed SQL>
(D)
begin savepoint before_loop; for rec in (select 1/0 from plch_one_row for update) loop null; end loop; exception when zero_divide then rollback to before_loop; end;
SQL> BEGIN 2 begin 3 savepoint before_loop; 4 for rec in (select 1 / 0 from plch_one_row for update) loop 5 null; 6 end loop; 7 exception 8 when zero_divide then 9 rollback to before_loop; 10 end; 11 plch_check_lock; 12 END; 13 / Not locked PL/SQL procedure successfully completed SQL>
答案ACD
(A)正確:如果用隱性游標(biāo)循環(huán),發(fā)生異常時(shí)鎖會(huì)被釋放
(B)不正確,如果用顯性游標(biāo)循環(huán),發(fā)生異常時(shí)鎖不會(huì)被釋放
(C)正確:異常被捕獲,顯式回滾到SAVE POINT, 因而鎖被釋放。
(D)正確:同A, 異常處理里的回滾相當(dāng)于什么也沒做。
感謝各位的閱讀,以上就是“數(shù)據(jù)庫中各種帶鎖游標(biāo)加鎖的時(shí)機(jī)分析”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對數(shù)據(jù)庫中各種帶鎖游標(biāo)加鎖的時(shí)機(jī)分析這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。