這篇文章主要講解了“數(shù)據(jù)庫中各種帶鎖游標(biāo)加鎖的時(shí)機(jī)分析”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“數(shù)據(jù)庫中各種帶鎖游標(biāo)加鎖的時(shí)機(jī)分析”吧!
創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司一直秉承“誠信做人,踏實(shí)做事”的原則,不欺瞞客戶,是我們最起碼的底線! 以服務(wù)為基礎(chǔ),以質(zhì)量求生存,以技術(shù)求發(fā)展,成交一個(gè)客戶多一個(gè)朋友!專注中小微企業(yè)官網(wǎng)定制,網(wǎng)站設(shè)計(jì)、成都網(wǎng)站設(shè)計(jì),塑造企業(yè)網(wǎng)絡(luò)形象打造互聯(lián)網(wǎng)企業(yè)效應(yīng)。
我建立了一個(gè)表并生成一行數(shù)據(jù):
create table plch_one_row (id number); insert into plch_one_row values (1); commit;
然后我建立一個(gè)過程來檢查我的表里這行數(shù)據(jù)是否被鎖住。我用的方法是在一個(gè)帶有自治事務(wù)的過程里試圖對(duì)這行進(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í)后,相信大家對(duì)數(shù)據(jù)庫中各種帶鎖游標(biāo)加鎖的時(shí)機(jī)分析這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!