免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 9009 | 回复: 4

DB2 行级锁,解决并发问题。 [复制链接]

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
发表于 2012-11-05 15:48 |显示全部楼层
db2 +c SQL 可以得到排他的行级锁。

问题是程序中不能写 DB2 +c 啊。如何才能在程序中实现行级的排他锁呢?

问题很急,求助。

论坛徽章:
0
发表于 2012-11-10 11:48 |显示全部楼层
db2 + c是禁止Auto commit, 不是你想的那个意思。如果更新一行,没有提交的话,DB2加的是IX表锁和X行锁。

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
发表于 2012-11-12 22:47 |显示全部楼层
回复 2# masaly


    我想要做的是A进程从表中获取一条记录,同时对这条记录加行级排他锁,直到这条记录被A进程修改并提交(或者是回滚)后,B进程才能对该行记录进行读取或者是修改。

不知道该如何实现。

论坛徽章:
0
发表于 2012-11-12 23:52 |显示全部楼层
这个要使用RS的隔离级别(中间件服务器默认隔离级别是RS,需要检查一下),并且用select for update。

Read Stability

Read Stability (RS) locks only those rows that an application retrieves within a unit of work. It ensures that any qualifying row read during a unit of work is not changed by other application processes until the unit of work completes, and that any row changed by another application process is not read until the change is committed by that process.


场景1:

session1:
db2 +c "select * from table where id=3 with rs for update";  --如果默认是RS,则不用加 with rs

session2:
db2 +c "select * from table where id=3 with rs for update";


这个时候如果我们用db2pd -wlocks可以看到,session2正在等待session1的行级U锁。


论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
发表于 2012-11-15 14:35 |显示全部楼层
@masaly

这个我试过了,现在有如下问题。

session1:
db2 +c "select * from table where id=3 with rs for update";  --如果默认是RS,则不用加 with rs

session2:
db2 +c "select * from table where id=3 with rs for update";

session1:
db2 “update table set id=id+1 where id=3"

此时 session1 的 update 操作被锁住,执行会一直等待,知道 session2 锁超时,造成检索失败后, session1 的update才能实现。

而我所需要的结果是,后面的 session1 的 update执行成功后,实现返回 session2 的 select  查询的结果,且这个结果是被修改后的,即 id = 4。

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP