锁(排它锁、悲观锁)


--查询中A使用 for update 当其他用户查询被锁的记录的时候,会跳过这条记录。如:
--A:
select * from test_zhi where khh = '001676786773' for update;  --默认行级锁
--B:
select * from test_zhi for update skip locked; --执行后就不会展示上个SQL锁住的数据


--for update nowait 如果A先查询数据时加了 for udpate wait, 当B查询时也加 for update wait, B就会报错,提示数据被锁
--如果B不加, B就会一直等待,直到A提交后,B才继续执行
--A:
select test_zhi_name, test_zhi_address from test_zhi where test_zhi_address = '01' for update nowait;
--B:
select test_zhi_name, test_zhi_address from test_zhi where test_zhi_address = '02' for update nowait;


--如果A使用for update nowait,B使用for update wait 3,表示3秒后提示‘资源正忙。。’
--A:
select * from test_zhi where khh = '001676786773' for update nowait;
--B:
select * from test_zhi where khh = '001676786773' for update wait 3;  --3S后提示资源正忙


--如果多表连接查询,使用for update, 会把所有的表都给锁住,如果使用for update of 表名.字段名,就只会锁住指定表名
--A:会锁住A、B两表张
select * from test_zhi1 a, test_zhi2 b where a.col5 = b.col5 for update;
--B:只会锁住A表
select * from test_zhi1 a, test_zhi2 b where a.col5 = b.col5 for update of a.col3;






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值