select for update和select for update wait和select for update nowait的区别
CREATE TABLE "TEST6"
(
"ID" VARCHAR2(30),
"NAME" VARCHAR2(30),
"AGE" NUMBER(2,0),
"SEX" VARCHAR2(2),
"ENAME" VARCHAR2(30),
"ADDTIME" DATE
)
insert into TEST6 (id, name, age, sex, ename, addtime) values ('', '张三', 18, null, 'zhangsan', to_date('14-03-2017 00:00:09', 'dd-mm-yyyy hh24:mi:ss'));
insert into TEST6 (id, name, age, sex, ename, addtime) values ('', '李四', null, '', 'Lisi', to_date('01-03-2017 02:00:00', 'dd-mm-yyyy hh24:mi:ss'));
insert into TEST6 (id, name, age, sex, ename, addtime) values ('', '王五', 20, '', 'wangwu', to_date('09-01-2017 08:55:00', 'dd-mm-yyyy hh24:mi:ss'));
insert into TEST6 (id, name, age, sex, ename, addtime) values ('', '赵六', 23, '', 'zhaoliu', to_date('03-03-2016 04:00:00', 'dd-mm-yyyy hh24:mi:ss'));
insert into TEST6 (id, name, age, sex, ename, addtime) values ('', '冯七', 22, null, 'fengqi', to_date('08-03-2017 12:00:01', 'dd-mm-yyyy hh24:mi:ss'));
CREATE TABLE "TEST8"
(
"ID" NUMBER,
"ORDERID" NUMBER,
"PRODUCTID" NUMBER,
"PRICE" NUMBER(10,2),
"QUANTITY" NUMBER
)
insert into TEST8 (id, orderid, productid, price, quantity) values (1, 1, 1, 6, 10);
insert into TEST8 (id, orderid, productid, price, quantity) values (2, 1, 2, 4, 5);
insert into TEST8 (id, orderid, productid, price, quantity) values (3, 1, 3, 10, 2);
insert into TEST8 (id, orderid, productid, price, quantity) values (4, 2, 1, 3, 6);
insert into TEST8 (id, orderid, productid, price, quantity) values (5, 2, 2, 4, 6);
以上是基础数据
在oracle中,如果只进行select语句的话,是不会进行加锁的,也就是oracle会返回当前时刻的结果集,即使这个时候可能有另外一个进程在修改当前结果集的数据,因为没有加锁,所以oracle还是会正常的返回当前时刻的结果集,不会有任何影响。
他们三个共同点:
当使用select for update 或者select for update wait或者.....,那么oralce会给符合where条件的数据行加上一个行级锁
1、select for update
但是如果你的select 语句加了for update,那么就不是上面这回事了,当oracle发现select的当前结果集中的一条或多条正在被修改(注意:当数据被修改时,此时的数据行是被加锁的),那么他就会等到当前当前结果集被修改完毕并且commit之后才进行select操作,并对结果集进行加锁。同样的,如果查询语句发出后,其他会话需要修改结果集中的一条(或几条数据)也许要等到查询结束(commit)之后,才可以执行修改操作。
代码如下:
新建SQL窗口1,(相当于新建一个session会话)
select * from test8 for update

for update 对整个结果集进行了加锁,意味着在当前session进行commit之前,任何其他的session进行update、delete、insert操作都会进行等待
新建SQL窗口2(相当于新建一个session会话)
update test8 set price=6 where ID=1

显示执行中,等待会话一的查询执行完成
现在我们将会话一的事务提交(commit)

会话二的update语句执行成功
2、select for update nowait
for update和for update nowait都会对查询到的当前结果集进行加锁,所不同的是,当有另外的会话在修改当前结果集中的数据,select for nowait所进行的查询操作不会进行等待,当发现结果集中的一些数据被加锁,立刻返回 “ORA-00054错误,内容是资源正忙, 但指定以 NOWAIT 方式获取资源”。测试代码如下:
新建一个SQL窗口1(相当于新建一个会话)
update test8 set price=3 where ID=1
更新test8表的一条数据,但是不进行commit操作
然后新建SQL窗口2(相当于新建一个会话)select for update nowait操作
select * from test8 for update nowait

总结分析:
因为会话一,并没有commit所以test8中的ID=1的行被加锁了,所以当会话二进行select for update nowait检索到ID=1的数据行被加锁了,就立刻返回 “ORA-00054错误,内容是资源正忙, 但指定以 NOWAIT 方式获取资源”的错误。
接下来我们对会话一进行commit操作,

在执行会话二的select查询,ok,可以查出来了,并且对当前数据集进行了加锁操作,其他会话想要进行修改操作,必须等到会话二commit之后

3、select for update wait
它也会对查询到的结果集进行加锁,select for update wait与select for update nowait不同的地方是,当有另外的会话对它的查询结果集中的某一行数据进行了加锁,那么它不会像nowait一样,立即返回"ORA-00054错误",而是它支持一个参数,设定等待的时间,当超过了设定的时间,那一行数据还处于加锁的状态,那么它也会返回“ORA-00054错误,内容是资源正忙, 但指定以 NOWAIT 方式获取资源”。测试代码如下:
首先新建SQL窗口1(相当于新建一个会话)执行update 语句,但是不进行commit操作,那么当前数据行将被lock
update test8 set price=3 where ID=1
接着新建SQL窗口2(相当于新建一个会话),在执行select for update wait 6,如果当前查询检索的数据集中,有被加锁了的行数据,那么等待6秒,如果6秒后,其他会话,还没有执行commit释放被加了锁的数据行的话,那么返回“ORA-00054错误,内容是资源正忙, 但指定以 NOWAIT 方式获取资源”。
select * from test8 for update wait 6

执行语句6秒后,报错。
最后对会话一(SQL窗口一)进行commit操作

紧接着执行会话二(SQL窗口二)中的sql语句,此时被加锁的数据行被释放

正常的检索除了数据行,当时当前数据集被加锁,其他会话想操作此数据集,必须等会话二中的事务commit之后,才可以进行修改
4、OF子句
在多表查询中如果需要对多表查询的结果集进行加锁,可以使用OF子句。
如果存在OF子句,那么就对满足OF子句的单表进行加锁,如果不存在OF子句就对整个结果集进行加锁,代码如下:
a、不使用OF子句
select a.ID,a.Name,b.price from test6 a
LEFT JOIN test8 b
ON a.ID=b.ID where b.ID>3
for update

没有进行commit操作,此时对test6和test8中的ID>3的数据行都进行了加锁,测试代码如下:
新建一个会话,执行以下语句:
select * from test6 for update skip locked

select * from test8 for update skip locked

测试结果证明,在没有OF子句的情况下,对多表查询的结果集进行select foe update,oracle会对满足where 条件的所有数据行进行加锁
b、使用OF子句
使用OF子句,那么oracle就会对满足OF子句的表进行加锁,在多表查询中。代码如下:
select a.ID,a.Name,b.price from test6 a
LEFT JOIN test8 b
ON a.ID=b.ID where b.ID>3
for update of a.ID

在不执行commit操作的情况,新建一个会话,执行一下语句:
select * from test6 for update skip locked

select * from test8 for update skip locked

比对测试结果,发现在OF子句的作用下,oracle对同时满足where子句(设置要加锁的数据行)和OF子句(主要设置加锁的表)的数据行进行了加锁。
select for update和select for update wait和select for update nowait的区别的更多相关文章
- 数据库(update tab1 set tab1.name=tab1.name+(select t2.name from tab2 t2 where t2.id=tab1.id))
有t1 和 t2 两个表,表中的数据和字段如下: 执行 如下SQL语句: update tab1 set tab1.name=tab1.name+(select t2.name from tab2 t ...
- [转]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 的话 ...
- 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的区别 - 转
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 的区 ...
- 下拉框——把一个select框中选中内容移到另一个select框中遇到的问题
在使用jQuery实现把一个select框中选中内容移到另一个select框中功能时遇到了一个问题,就是点击按钮时内容可以到另一个select框中,但是到了另一个select框中的内容却很快闪退回原来 ...
- Mysql update error: Error Code: 1175. You are using safe update mode and you tried to update a table
Mysql update error: Error Code: 1175. You are using safe update mode and you tried to update a table ...
- select change事件给其它元素赋值,本select的value或tex
select change事件给其它元素赋值,本select的value或textonchange='$("#areaname").val($("option:selec ...
随机推荐
- 团体程序设计天梯赛L2-002 链表去重 2017-03-22 18:12 25人阅读 评论(0) 收藏
L2-002. 链表去重 时间限制 300 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一个带整数键值的单链表L,本题要求你编写程序,删除 ...
- 洛谷P2634 [国家集训队]聪聪可可 (点分治)
题目描述 聪聪和可可是兄弟俩,他们俩经常为了一些琐事打起来,例如家中只剩下最后一根冰棍而两人都想吃.两个人都想玩儿电脑(可是他们家只有一台电脑)……遇到这种问题,一般情况下石头剪刀布就好了,可是他们已 ...
- Android-bindService远程服务(Aidl)-传递对象
之前上一篇讲解到本地服务,本地服务只能在自身APP中Activity访问Service,调用Service里面到方法等操作 如果想A应用访问B应用里面的方法,属于跨进程调用,如果Android不特供这 ...
- [LeetCode 题解]: Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- DotNetBar 中 SuperGridControl 加载数据、获取数据、设置样式
1.加载数据 构建列 //加载列 GridColumn gd = new GridColumn(); gd.Name = "第1"; gd.HeaderText = "第 ...
- Nginx使用
1. 基本使用 分linux和windows版 windows版可以直接双击exe运行,默认配置为80端口,只有两个页面 html目录下为页面.css.js等代码文件 conf目录下为配置文件 主要的 ...
- ORM之PetaPoco
近端时间从推酷app上了解到C#轻微型的ORM框架--PetaPoco.从github Dapper 开源项目可以看到PetaPoco排第四 以下是网友根据官方介绍翻译,这里贴出来. PetaPoco ...
- WebSerervice webapi使用
WebSerervice webapi使用 1.传json参数: 2.返回json数据: 3.权限控制: Authorize特性:必须经过认证,请求头必须具有token信息 4.路由: 5.过滤器: ...
- 用Visual Studio 2015成功编译、发布UMDF驱动到目标机!!
开发工具:Visual Studio 2015企业版 主 机:windows10 X64企业版,主机是安装了Visual Studio 2015的操作系统,主要进行驱动开发和调试. 目 标 ...
- HTTP响应状态码参考
HTTP响应状态码参考: 1xx:信息 Continue 服务器仅接收到部分请求,但是一旦服务器并没有拒绝该请求,客户端应该继续发送其余的请求. Switching Protocols 服务器转换协议 ...