for-update与for-update nowait
1、for update 和 for update nowait 的区别:
首先一点,如果只是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,则抛出相应的异常)
2、for update nowait 与 for update 的目的:
锁定表的所有行,排斥其他针对这个表的写操作。确保只有当前事务对指定表进行写操作。
for update nowait和 for update的区别:
别的事务要对这个表进行写操作时,是等待一段时间还是马上就被数据库系统拒绝而返回.制定采用nowait方式来进行检索,所以当发现数据被别的session锁定中的时候,就会迅速返回ORA-00054错误,内容是资源正忙, 但指定以 NOWAIT 方式获取资源。所以在程序中我们可以采用nowait方式迅速判断当前数据是否被锁定中,如果锁定中的话,就要采取相应的业务措施进行处理。
如何理解上面的话.
开启一会话 (就是开一个sqlwindow)
select empno,ename from emp for update nowait where
empno='7369';
得到下面结果集:
empno ename
7369 smith
开启另一会话
select empno,ename from emp for update nowait where
empno="7369"
返回RA-00054错误,内容是资源正忙, 但指定以
NOWAIT 方式获取资源
上面会话都提交commit;
~~~~~~~~~~~~~~~~~~~~~
开启一会话,
select empno,ename from emp for update where
empno='7369';
得到下面结果集:
empno ename
7369 smith
开启另一会话
select empno,ename from emp for update where
empno="7369"
阻塞,不返回错误。
提交第一个会话,第二个回话自动执行
提交第二个会话
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
for update: 当第一个session最后commit或者rollback之后,第二个session中的检索结果就是自动跳出来,并且也把数据锁定住.
开启一会话:
select empno,ename from emp for
update where empno="7369";
得到下面结果集:
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照样不能对数据进行锁定更新等等。
同上没区别
for-update与for-update nowait的更多相关文章
- 【JAVA】FOR UPDATE 和 FOR UPDATE NOWAIT 区别 (转)
1.for update 和 for update nowait 的区别:首先一点,如果只是select 的话,Oracle是不会加任何锁的,也就是Oracle对 select 读到的数据不会有任何限 ...
- [转]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 的话 ...
- for update和for update nowait的区别和使用
首先,for update 和for update nowait 是对操作的数据行进行加锁,在事务提交前防止其他操作对数据的修改. 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 ...
- sql: oracle, for update和for update nowait的区别
1. oracle for update和for update nowait的区别 http://www.cnblogs.com/quanweiru/archive/2012/11/09/276222 ...
- 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 的区 ...
随机推荐
- IBM XIV
参考:https://www.doit.com.cn/p/author/xigua 参考:http://www.doit.com.cn/p/196056.html 图片说明: IBM XIV存储系统采 ...
- python-----将图片与标注的xml坐标水平翻转
我们做机器学习的时候,总会用到很多训练集,然后我们的数据比较少的时候,就可以将图片翻转标注.代码如下: #!/usr/bin/env python # -*- coding: utf-8 -*- # ...
- synchronized 和 Lock 有什么区别?(未完成)
synchronized 和 Lock 有什么区别?(未完成)
- Java基础-异常处理机制 及异常处理的五个关键字:try/catch/finally/throw /throws
笔记: /** 异常处理机制: 抓抛模型 * 1."抛", 一旦抛出,程序终止! printStackTrace()显示异常路径! * 2."抓", 抓住异常 ...
- 10—mybatis 通用mapper插件 pagehelper 分页
spring boot真的太好用了,大家以后多多使用,今天来说说pagehelper 来做mybatis分页,我用的是spring boot 做的开发,后面会把源码发出来. pagehelper(ht ...
- python yaml文件内容的读取
示例: (1)host_header.yaml 文件中的内容 host: https://testapp.goodiber.com/v2/ #dev1的测试环境域名 #请求接口的请求头中的共用参数 ...
- Python+request+ smtplib 测试结果html报告邮件发送(上)《五》
此方法通用适合所有邮箱的使用,只需注意几个点,如下: QQ邮箱.其他非QQ邮箱的写法,区别点如下: #--------------------------使用腾讯企业邮箱作为发件人的操作如下----- ...
- centos7下redis和php-redis安装
centos7下redis安装和php-redis扩展安装 //一直yes就可以了 yum install redis //配置 whereis redis.conf vi /etc/redis.co ...
- Java锁机制总结
锁是用于控制多线程对共享资源的访问. Java中的锁可以分为内置锁与显式锁Lock.其中内置锁指synchronized关键字. Synchronized synchronized可以修饰方法或代码块 ...
- java+web+多级文件上传
文件夹数据库处理逻辑 publicclass DbFolder { JSONObject root; public DbFolder() { this.root = new JSONObject(); ...