“FOR UPDATE OF” 是“行锁”(locks on the row) -- 不存在字段加锁!
这“行锁”的作用
1.始于一个CURSOR 的OPEN时候,
2.终于一个完整的传送 -- COMMIT 或ROLLBACK,而不是CURSOR 的完结(CLOSE your_cursor_name) 。
3>.若有两个CURSOR 对同一“行”(row)中任意字段(column)作出更改(UPDATE),
时间上的后一CURSOR会一直等待,至第一个传送完结为止 -- COMMIT 或ROLLBACK。
4.若不被有效处理,死锁就成形。
例如:执行以下程序于两个session上,第二个session会一直处于等待。。。直至
第一个session执行COMMIT 或ROLLBACK为止。
declare
-- query emp.name
cursor cur_emp
is
select empno, ename, job
from emp
where empno = 7369
for update of ename;
begin
for return_cur in cur_emp
loop
update emp
set ename = 'LHG'
where current of cur_emp;
end loop;
end;
5.防止4的方法之一是加NOWAIT在FOR UPDATE之后。如此,第二个session不会一
直等下去,而是出现ORA-00054 [resource busy and acquire with NOWAIT specified]的讯息。
由此,我自己的FOR UPDATE习惯是,
a.NOWAIT定然跟FOR UPDATE之后。
b.COMMIT必需存在程序结尾。以防死锁成形。
c. EXCEPTION里的ROLLBACK是最基本的需要。
在多数情况下,提取循环中所完成的处理都会修改由游标检查出的行,PL/SQL提供了进行这样处理的一种语法。 这种语法包括两部分——在游标声明部分的FOR UPDATE子句和在UPDATE或DELETE语句中的WHERE CURRENT OF 子句。 通常,SELECT操作将不会对正处理的行执行任何锁定设置,这使得连接到该数据库的其他会话可以改变正在选择的数据。 当确定了活动集以后,在执行OPEN的时刻,ORACLE会截取下该表的一个快照。在此时刻以前所提交的任何更改操作都会在活动集中反映出来。在此时刻以后所进行的任何更改操作,即使已经提交了它们,都不会被反映出来,除非将该游标重新打开(数据可能不一致)。
但是使用FOR UPDATE子句,在OPEN返回以前的活动集的相应行上会加上互斥锁,这些锁会避免其他的会话对活动集中的行进行更改。 直到该语句的事务被commit语句或rollback语句结束为止。
for update of 和for update区别
1 select * from TTable1 for update 锁定表的所有行,只能读不能写
2 select * from TTable1 where pkid = 1 for update 只锁定pkid=1的行
3 select * from Table1 a join Table2 b on a.pkid=b.pkid for update 锁定两个表的所有记录
4 select * from Table1 a join Table2 b on a.pkid=b.pkid where a.pkid = 10 for update 锁定两个表的中满足条件的行
5. select * from Table1 a join Table2 b on a.pkid=b.pkid where a.pkid = 10 for update of a.pkid 只锁定Table1中满足条件的行
总结: for update 是把所有的表都锁点 for update of 根据of 后表的条件锁定相对应的表
关于nowait(如果一定要用FOR UPDATE,建议加上nowait)
当有LOCK冲突时会提示错误并结束statement而不是在那里等待(比如:要查的行已经被其它事务锁了,当前的锁事务与之冲突,加上nowait,当前的事务会结束会提示错误并立即结束 statement再等待).
关于 where current of
使用FOR UPDATE子句是在游标检索时锁定检索到的行,并对应数据库中的物理行,其中后半句是核心。如果是真正的UPDATE,应该在指明UPDATE子句后使用where current of . 确切的说for update 是为update做检索准备。
如果你想删除或者更新被Select For Update引用的记录,你可以使用Where Current Of语句。
Where Current Of语句允许你更新或者是删除最后由cursor取的记录。
分享到:
相关推荐
### 数据库Oracle锁:FOR UPDATE OF与FOR UPDATE的区别 在Oracle数据库中,为了确保数据的一致性和准确性,尤其是在多用户环境中进行并发操作时,锁机制是必不可少的一部分。本文将详细介绍`FOR UPDATE`与`FOR ...
sqlalchemy 对于行级锁有两种实现方式,with_lockmode(self, mode): 和 with_for_update(self, read=False, nowait=False, of=None),前者在sqlalchemy 0.9.0 被废弃,用后者代替。所以我们使用with_for_update ! 看...
通常情况下,我们会使用以下SQL语句来更新字段值: ...for x in xrange(10): sql = ''' UPDATE mytable SET myfield='value' WHERE other_field='other_value'; ''' 这种方法并没有什么任何错误,
触发器---FOR INSERT与INSTEAD OF 触发器是一种特殊的存储过程,它可以自动执行某些操作以响应特定的事件,如INSERT、UPDATE、DELETE等。触发器可以帮助我们自动执行一些必要的操作,以确保数据的一致性和完整性。...
in terms of reliability, accuracy or fitness for purpose. You assume the entire risk of direct or indirect, consequential or inconsequential results from the correct or incorrect usage of this ...
例如:`BEGIN FOR cr IN (SELECT * FROM t_join_situation WHERE year='2011') LOOP UPDATE t_join_situation SET join_state='1' WHERE current of cr; END LOOP`。这种方式适用于大批量数据的更新,但需要注意游标...
`Updating Form from Another Thread without Creating Delegates for Every Type of Update`这个主题探讨的是如何在不为每种类型的更新创建单独委托的情况下,从其他线程安全地更新UI。 通常,当你尝试在非UI线程...
After messing around a bit with the perl tools available for extracting Huawei update.app files, i got the idea to create an own (windows) tool. Requirements .Net Framework 3.5 Install Extract the...
ARM Compiler v5.01, Build 94, is intended for use as an update for ARM Compiler 5 and ARM DS-5 products, and must be used with a suitable license from one of those products. Multiple feature releases ...
Hotfix 1 for RAD Studio, Delphi XE5 Update 2 Available to registered users of Delphi XE5, RAD Studio XE5, and Embarcadero All-Access XE This hotfix supplies some missing lib file when installing ...
这一法规是根据1958年联合国关于协调统一机动车及其零部件技术规定的协议(Agreement Concerning the Adoption of Harmonized Technical United Nations Regulations for Wheeled Vehicles, Equipment and Parts ...
TYPE col_table IS TABLE OF table_name.column_name%TYPE INDEX BY PLS_INTEGER; bulk_col col_table; BEGIN SELECT column_name BULK COLLECT INTO bulk_col FROM table_name WHERE condition; FORALL i IN ...
#### 技术方案与算法描述 ##### 区域基础方法 本文提出的道路提取算法采用了一种区域基础的方法,首先使用归一化切割(Normalized Cuts)算法结合颜色和边缘标准对图像进行分割。归一化切割是一种基于图论的分割...
ARM Compiler v5.05, Build 169, is intended for use as an update for ARM Compiler 5 products, and must be used with a suitable license from one of these products. This update is not suitable for ...
The GNU Embedded Toolchain for Arm is a ready-to-use, open source suite of tools for C, C++ and Assembly programming targeting Arm Cortex-M and Cortex-R family of processors. It includes the GNU ...
RTL - VCL - THIRD PARTY - BOLD FOR DELPHI * VERIFYING THAT THE UPDATE WAS SUCCESSFUL * FILES INSTALLED BY THIS UPDATE =======================================================INSTALLING THIS UPDATE* ...
Please, update to the new Skype 8.x, which is ready for you with lots of improvements at Skype.com. In case you hit any issues, please check known issues, system requirements, or post your questions...
Please, update to the new Skype 8.x, which is ready for you with lots of improvements at Skype.com. In case you hit any issues, please check known issues, system requirements, or post your questions...
Delphi DirectX headers adaptation followed by Borland and JEDI standarts for all (up to 9.0 from April 2007 DirectX SDK update) versions of DirectDraw, Direct3D, DirectInput, DirectSound, DirectMusic,...