oracle-select for update nowait 与 select for update 差别


oracle--select for update nowait 与 select for update 区别
nowait的含义很多人都会误解为“不用等待,立即执行”。但实际上该关键字的含义是“不用等待,立即返回”
如果当前请求的资源被其他会话锁定时,会发生阻塞,nowait可以避免这一阻塞,因为
If another user is in the process of modifying that row, we will get an ORA‐00054
Resource Busy error. We are blocked and must wait for the other user to finish with
it.
可以实验下,我用pl/sql developer锁定表game
SQL> select * from game where game_id =1;
返回一条记录

SQL> select * from game where game_id=1 for update nowait;
select * from game where game_id=1 for update nowait
             *
ERROR位于第1行:
ORA-00054:资源正忙,要求指定NOWAIT
使用NOWAIT关键字,会报ORA‐00054的错误


如何来查看是什么资源造成这样的情况呢?并且怎么解决呢?
查看锁定的对象,用户和会话
SQL> select lo.oracle_username,do.object_name,s.logon_time,lo.process,s.sid as s
ession_id
 2 from v$locked_object lo,v$session s,dba_objects do
 3 where lo.session_id = s.sid and do.object_id = lo.OBJECT_ID
 4 /
ORACLE_USERNAME
------------------------------
OBJECT_NAME
--------------------------------------------
LOGON_TIME         PROCESS     SESSION_ID
------------------- ------------ ----------
NBA---用户名称
GAME---操作的对象
2009-08-04 10:55:15---登录的时间     840:5176   10
 


使用as sysdba
根据sid查看具体的sql语句
selectsql_textfromv$session a,v$sqltext_with_newlines b
whereDECODE(a.sql_hash_value,0, prev_hash_value, sql_hash_value)=b.hash_value
 anda.sid=10;
begin :id := sys.dbms_transaction.local_transaction_id; end;

kill session
SQL> select sid,serial# from v$session where sid =10;
 
      SID   SERIAL#
---------- ----------
       10        23
SQL> alter system kill session '10,23';

系统已更改。

select * from game where game_id=1 for update nowait;
有数据返回了

当两个用户同时更新同一条记录是, 使用select for update,后执行者,会被阻塞,而使用select for update  nowait 则会抛出:ORA-00054 resource busy and acquire with NOWAIT specified 异常,告之用户这一行已经锁定。

原文来源:http://www.blogjava.net/parable-myth/archive/2010/11/05/337350.html?opt=admin
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值