Oracle Select for update用法

Oracle Select for update用法

通常情况下Select不会对资料加锁,不会影响其他Session执行DML和DDL,借助for update子句就可以实现对资料加锁保护操作。當只允許一個session進行update的時候, for update十分有用.
在select … for update之后,可以使用of子句对select的特定资料进行加锁操作,不适用of子句表示在select所有的表加锁。
select * from test for update; 會對table test進行加鎖. 此時只允許當前的session對已經存在的資料進行更新. 但其它session仍可以進行insert的操作,只是不允许进行update操作。

select * from Table1 a join Table2 b on a.pkid=b.pkid where a.pkid = 10 for update of a.pkid 只鎖定Table1中滿足條件的行, 這就是使用of子句的作用. 比較常用於多個表的操作.

加入for update之後,Oracle就要求啟動一個新事務,嘗試對資料進行加鎖。如果當前已經被加鎖,預設的行為必然是block等待。使用nowait子句的作用就是避免進行等待,當發現請求加鎖資源被鎖定未釋放的時候,直接報錯返回。如果不使用nowait或wait子句, 新的加鎖請求會一直hang住, 直到原來的commit或rollback.

select * from test where a=2 for update nowait;
ERROR at line 1:
ORA-00054: resource busy and acquire with NOWAIT specified or timeout expired

或者
select * from test where a=2 for update wait 3; 如果3秒內還是無法加鎖則返回錯誤.

在一個session內可以多次進行select XX for update,然後只需要commit或rollback一次即可釋放.

引用

oracle for update和for update nowait的区别

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值