for update 和 for update nowait

for update nowait和 for update的区别:

别的事务要对这个表进行写操作时,是等待一段时间还是马上就被数据库系统拒绝而返回.

制定采用nowait方式来进行检索,所以当发现数据被别的session锁定中的时候,就会迅速返回ORA-00054错误,

内容是资源正忙, 但指定以 NOWAIT 方式获取资源。

所以在程序中我们可以采用nowait方式迅速判断当前数据是否被锁定中,如果锁定中的话,就要采取相应的业务措施进行处理。 





Oracle 的for update行锁 
SELECT...FOR UPDATE 语句的语法如下: 
SELECT ... FOR UPDATE [OF column_list][WAIT n|NOWAIT][SKIP LOCKED]; 
其中: 
OF 子句用于指定即将更新的列,即锁定行上的特定列。 
WAIT 子句指定等待其他用户释放锁的秒数,防止无限期的等待。 
“使用FOR UPDATE WAIT”子句的优点如下: 
1防止无限期地等待被锁定的行; 
2允许应用程序中对锁的等待时间进行更多的控制。 
3对于交互式应用程序非常有用,因为这些用户不能等待不确定 
4 若使用了skip locked,则可以越过锁定的行,不会报告由wait n 引发的‘资源忙’异常报告


示例: 
create table t(a varchar2(20),b varchar2(20)); 
insert into t values('1','1'); 
insert into t values('2','2'); 
insert into t values('3','3'); 
insert into t values('4','4'); 
现在执行如下操作: 
在plsql develope中打开两个sql窗口, 
在1窗口中运行sql 
select * from t where a='1' for update; 
在2窗口中运行sql1 
1. select * from t where a='1'; 这一点问题也没有,因为行级锁不会影响纯粹的select语句 
再运行sql2 
2. select * from t where a='1' for update; 则这一句sql在执行时,永远处于等待状态,除非窗口1中sql被提交或回滚。 
如何才能让sql2不等待或等待指定的时间呢? 我们再运行sql3 
3. select * from t where a='1' for update nowait; 则在执行此sql时,直接报资源忙的异常。 
若执行 select * from t where a='1' for update wait 6; 则在等待6秒后,报 资源忙的异常。 
如果我们执行sql4 
4. select * from t where a='1' for update nowait skip Locked; 则执行sql时,即不等待,也不报资源忙异常。 
现在我们看看执行如下操作将会发生什么呢? 
在窗口1中执行: 
select * from t where rownum<=3  for update nowait skip Locked; 
在窗口2中执行: 
select * from t where rownum<=6 for update nowait skip Locked; 
结果:显示第四行数据 




对比区别: 
1  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 后表的条件锁定相对应的表




关于oracle中的select...for update of columns 

问题,如下:

select * from emp where empno = 7369 for update; 

会对表中员工编号为7369的记录进行上锁。其他用户无法对该记录进行操作,只能查询。

select * from emp where empno = 7369 for update of sal;

这条语句是不是意味着只对表中的7369 这一行的sal字段的数据进行了上锁,

其他数据则可以被其他用户做更新操作呢?

测试结果为二条语句的效果是一样的。其他用户对整行都无法更新,
那么是不是意味着 for update of columns这句没有什么意义呢?



从单独一张表的操作来看,上面二条语句的效果确实是相同的。但是如果涉及到多表操作的时候 for update of columns就起到了非常大的作用了。现假定有二个用户,scott和mm。


scott执行语句:

select * from emp e,dept d where e.deptno = d.deptno for update; --对二张表都进行了整表锁定 

mm执行语句:

select * from scott.dept for update wait 3; --试图锁定scott用户的dept表



结果是: 
ERROR 位于第 1 行: 
ORA-30006: 资源已被占用; 执行操作时出现 WAIT 超时


现在,scott用户先进行解锁rollback,再在for update语句后面加上of columns,进行测试


scott执行语句:select * from emp e,dept d where e.deptno = d.deptno for update of sal ; 
mm执行语句:select * from scott.dept for update wait 3;


结果是: 
成功锁定了dept表的数据.


mm再次执行语句:select * from scott.emp for update wait 3;


结果是: 
ERROR 位于第 1 行: 
ORA-30006: 资源已被占用; 执行操作时出现 WAIT 超时


通过这段代码案例,我们可以得到结论,for update of columns 用在多表连接锁定时,

可以指定要锁定的是哪几张表,而如果表中的列没有在for update of 后面出现的话,

就意味着这张表其实并没有被锁定,其他用户是可以对这些表的数据进行update操作的。

这种情况经常会出现在用户对带有连接查询的视图进行操作场景下。用户只锁定相关表的数据,

其他用户仍然可以对视图中其他原始表的数据来进行操作。 

   

猜你喜欢

转载自blog.csdn.net/guogrowth/article/details/45694099