sql: oracle, for update和for update nowait的区别
1. oracle for update和for update nowait的区别 http://www.cnblogs.com/quanweiru/archive/2012/11/09/2762223.html
首先一点,如果只是select 的话,Oracle是不会加任何锁的,也就是Oracle对 select 读到的数据不会有任何限制,虽然这时候有可能另外一个进程正在修改表中的数据,并且修改的结果可能影响到你目前select语句的结果,但是因为没有锁,所以select结果为当前时刻表中记录的状态。
如果加入了for update, 则Oracle一旦发现(符合查询条件的)这批数据正在被修改,则不会发出该select语句查询,直到数据被修改结束(被commit),马上自动执行这个select语句。 同样,如果该查询语句发出后,有人需要修改这批数据(中的一条或几条),它也必须等到查询结束后(commit)后,才能修改。( 等待别人 + 让别人等待)
for update nowait和 for update 都会对所查询到得结果集进行加锁,所不同的是,如果另外一个线程正在修改结果集中的数据,for update nowait 不会进行资源等待,只要发现结果集中有些数据被加锁,立刻返回 “ORA-00054错误,内容是资源正忙, 但指定以 NOWAIT 方式获取资源”。
for update 和 for update nowait 加上的是一个行级锁,也就是只有符合where条件的数据被加锁。如果仅仅用update语句来更改数据时,可能会因为加不上锁而没有响应地、莫名其妙地等待,但如果在此之前,for update NOWAIT语句将要更改的数据试探性地加锁,就可以通过立即返回的错误提示而明白其中的道理,或许这就是For Update和NOWAIT的意义之所在。
经过测试,以for update 或 for update nowait方式进行查询加锁,在select的结果集中,只要有任何一个记录在加锁,则整个结果集都在等待系统资源(如果是nowait,则抛出相应的异常)
如何理解上面的话.
开启一会话 (就是开一个sqlwindow)
select empno,ename from emp where empno='7369' for update nowait ;
得到下面结果集:
empno ename
7369 smith
开启另一会话
select empno,ename from emp where empno='7369' for update nowait ;
返回RA-00054错误,内容是资源正忙, 但指定以 NOWAIT 方式获取资源
上面会话都提交commit;
~~~~~~~~~~~~~~~~~~~~~
开启一会话,
select empno,ename from emp where empno='7369' for update ;
得到下面结果集:
empno ename
7369 smith
开启另一会话
select empno,ename from emp where empno='7369' for update; 阻塞,不返回错误。
提交第一个会话,第二个回话自动执行
提交第二个会话
for update: 当第一个session最后commit或者rollback之后,第二个session中的检索结果就是自动跳出来,并且也把数据锁定住.
开启一会话:
select empno,ename from emp where empno="7369" for update;
得到下面结果集:
empno ename
7369 smith
开启另一个会话,
update emp set ename='ALLEN' where empno="7396";
阻塞。
提交第一个会话,update 语句执行
再开启一会话
update emp set ename="SMITH" where empno='7396';
同样阻塞,虽然第一个会话因为提交而释放了锁,但是第二个会话中的update 又给这一行加锁了;
for update nowait:当你第一个session放开锁定以后,第二个session才能正常运行。当你第二个session语句运行后,数据又被你第二个session语句锁定住了,这个时候只要你第二个session语句后还没有commit,别的session照样不能对数据进行锁定更新等等。
对比区别:
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 后表的条件锁定相对应的表
sql: oracle, for update和for update nowait的区别的更多相关文章
- [转]oracle for update和for update nowait的区别
1概念小结:(针对以下引用区域内容) 1.1 普通select语句不加锁. 1.2 for update和for update nowait都试图将符合条件的数据加上行级锁.用于排斥其他针对这个表的写 ...
- Oracle 中 for update 和 for update nowait 的区别
原文出处http://bijian1013.iteye.com/blog/1895412 一.for update 和 for update nowait 的区别 首先一点,如果只是select 的话 ...
- oracle for update和for update nowait(for update wait)的区别
1.for update 和 for update nowait 的区别: 1.oracle 中执行select 操作读取数据不会有任何限制,当另外一个进程在修改表中的数据,但是并没有commit,所 ...
- oracle for update和for update nowait的区别 - 转
1.for update 和 for update nowait 的区别: 首先一点,如果只是select 的话,Oracle是不会加任何锁的,也就是Oracle对 select 读到的数据不会有任何 ...
- oracle for update和for update nowait 的区别
原文地址:http://www.cnblogs.com/quanweiru/archive/2012/11/09/2762223.html 1.for update 和 for update nowa ...
- Oracle 中for update和for update nowait的区别
http://www.cnblogs.com/quanweiru/archive/2012/11/09/2762223.html 1.for update 和 for update nowait 的区 ...
- oracle for update和for update nowait
原文地址:http://www.cnblogs.com/quanweiru/archive/2012/11/09/2762223.html 1.for update 和 for update nowa ...
- 【JAVA】FOR UPDATE 和 FOR UPDATE NOWAIT 区别 (转)
1.for update 和 for update nowait 的区别:首先一点,如果只是select 的话,Oracle是不会加任何锁的,也就是Oracle对 select 读到的数据不会有任何限 ...
- select for update和select for update wait和select for update nowait的区别
CREATE TABLE "TEST6" ( "ID" ), "NAME" ), "AGE" ,), "SEX ...
随机推荐
- Uva 225 Golygons
这道题如果直接用Dfs,运气好的话是可以直接过的. 但如果要在Dfs的基础上加快速度,剪枝是必不可少的. 我的剪枝策略: 1.当前点(x,y)回到出发点至少需要 |x| +| y| 步,如果剩余的步数 ...
- 《Clean Code》重点内容总结
读书笔记请见Github博客:http://wuxichen.github.io/Myblog/reading/2014/10/06/CleanCode.html
- Currency System in Geraldion (Codeforces 560A)
A Currency System in Geraldion Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64 ...
- 自定义ActionBar背景(分别针对3.0以下和3.0以上的版本)
官方原文:http://developer.android.com/training/basics/actionbar/styling.html 针对3.0以上的版本: <?xml versio ...
- [POJ 1521]--Entropy(哈夫曼树)
题目链接:http://poj.org/problem?id=1521 Entropy Time Limit: 1000MS Memory Limit: 10000K Description A ...
- [转] Ant 编译 Android 项目为 Apk 实战, 常见问题解决
补充,自行安装Ant, 配置ant环境变量, 在android 项目 根目录下使用 android update project 可以自动生成 build.xml 和 local.peoperties ...
- 将时间显示为“刚刚”“n分钟/小时前”等
在很多场合为了显示出信息的及时性,一般会将时间显示成“刚刚”,“5分钟前”,“3小时前”等,而不是直接将时间打印出来.比如微博,SNS类应用就最长用到这个功能.而一般存储在数据库中的时间格式为 Uni ...
- HDU2138 随机素数测试 Miller-Rabin算法
题目描述 Give you a lot of positive integers, just to find out how many prime numbers there are.. In eac ...
- JavaScript:获取系统当前时间,构造格式化的字符串
var getNowFormatDate = function() { var date = new Date(); var seperator1 = "-"; ...
- php内存分析
1.一般来说,php倒不需要进行内存分析,但是遇到大循环时内存吃紧时就得要进行内存分析了,看看在哪里吃掉了内存. $m1 = memory_get_usage(); $m2 = memory_get_ ...