您好,登錄后才能下訂單哦!
這篇文章主要講解了“數據庫中各種帶鎖游標加鎖的時機分析”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“數據庫中各種帶鎖游標加鎖的時機分析”吧!
我建立了一個表并生成一行數據:
create table plch_one_row (id number); insert into plch_one_row values (1); commit;
然后我建立一個過程來檢查我的表里這行數據是否被鎖住。我用的方法是在一個帶有自治事務的過程里試圖對這行進行加鎖。
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; /
下列的選項中,哪些可以用來代替下面這個塊中的/* code */注釋,從而執行之后會顯示"Not locked"? 你可以假定在執行之前表上沒有鎖。
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)正確:如果用隱性游標循環,發生異常時鎖會被釋放
(B)不正確,如果用顯性游標循環,發生異常時鎖不會被釋放
(C)正確:異常被捕獲,顯式回滾到SAVE POINT, 因而鎖被釋放。
(D)正確:同A, 異常處理里的回滾相當于什么也沒做。
感謝各位的閱讀,以上就是“數據庫中各種帶鎖游標加鎖的時機分析”的內容了,經過本文的學習后,相信大家對數據庫中各種帶鎖游標加鎖的時機分析這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。