这篇文章主要讲解了“数据库中各种带锁游标加锁的时机分析”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“数据库中各种带锁游标加锁的时机分析”吧!

我建立了一个表并生成一行数据:

createtableplch_one_row(idnumber);insertintoplch_one_rowvalues(1);commit;

然后我建立一个过程来检查我的表里这行数据是否被锁住。我用的方法是在一个带有自治事务的过程里试图对这行进行加锁。

CREATEORREPLACEPROCEDUREplch_check_lockASPRAGMAAUTONOMOUS_TRANSACTION;resource_busyEXCEPTION;PRAGMAEXCEPTION_INIT(resource_busy,-54);l_idplch_one_row.id%TYPE;BEGINSELECTidINTOl_idFROMplch_one_rowFORUPDATENOWAIT;DBMS_OUTPUT.put_line('Notlocked');COMMIT;EXCEPTIONWHENresource_busyTHENDBMS_OUTPUT.put_line('Locked');END;/

下列的选项中,哪些可以用来代替下面这个块中的/* code */注释,从而执行之后会显示"Not locked"? 你可以假定在执行之前表上没有锁。

BEGIN/*code*/plch_check_lock;END;/

(A)

beginforrecin(select1/0fromplch_one_rowforupdate)loopnull;endloop;exceptionwhenzero_dividethennull;end;

SQL>BEGIN2begin3forrecin(select1/0fromplch_one_rowforupdate)loop4null;5endloop;6exception7whenzero_dividethen8null;9end;10plch_check_lock;11END;12/NotlockedPL/SQLproceduresuccessfullycompletedSQL>

(B)

declarecursorcurisselect1/0fromplch_one_rowforupdate;beginforrecincurloopnull;endloop;exceptionwhenzero_dividethennull;end;

SQL>BEGIN2declare3cursorcuris4select1/0fromplch_one_rowforupdate;5begin6forrecincurloop7null;8endloop;9exception10whenzero_dividethen11null;12end;13plch_check_lock;14END;15/LockedPL/SQLproceduresuccessfullycompletedSQL>

(C)

declarecursorcurisselect1/0fromplch_one_rowforupdate;beginsavepointbefore_loop;forrecincurloopnull;endloop;exceptionwhenzero_dividethenrollbacktobefore_loop;end;

SQL>BEGIN2declare3cursorcuris4select1/0fromplch_one_rowforupdate;5begin6savepointbefore_loop;7forrecincurloop8null;9endloop;10exception11whenzero_dividethen12rollbacktobefore_loop;13end;14plch_check_lock;15END;16/NotlockedPL/SQLproceduresuccessfullycompletedSQL>

(D)

beginsavepointbefore_loop;forrecin(select1/0fromplch_one_rowforupdate)loopnull;endloop;exceptionwhenzero_dividethenrollbacktobefore_loop;end;

SQL>BEGIN2begin3savepointbefore_loop;4forrecin(select1/0fromplch_one_rowforupdate)loop5null;6endloop;7exception8whenzero_dividethen9rollbacktobefore_loop;10end;11plch_check_lock;12END;13/NotlockedPL/SQLproceduresuccessfullycompletedSQL>

答案ACD

(A)正确:如果用隐性游标循环,发生异常时锁会被释放
(B)不正确,如果用显性游标循环,发生异常时锁不会被释放
(C)正确:异常被捕获,显式回滚到SAVE POINT, 因而锁被释放。
(D)正确:同A, 异常处理里的回滚相当于什么也没做。

感谢各位的阅读,以上就是“数据库中各种带锁游标加锁的时机分析”的内容了,经过本文的学习后,相信大家对数据库中各种带锁游标加锁的时机分析这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!