Chinaunix

标题: [db2]锁问题求教 [打印本页]

作者: ivhb    时间: 2011-03-22 22:46
标题: [db2]锁问题求教
操作系统:

  1. /opt/ibm/db2/V9.1/bin>uname -a
  2. Linux yon 2.6.9-67.EL #1 Wed Nov 7 13:43:31 EST 2007 x86_64 x86_64 x86_64 GNU/Linux
复制代码
数据库:

  1. /opt/ibm/db2/V9.1/bin>db2level
  2. DB21085I  Instance "db2inst1" uses "64" bits and DB2 code release "SQL09013"
  3. with level identifier "01040107".
  4. Informational tokens are "DB2 v9.1.0.3", "s070719", "MI00203", and Fix Pack
  5. "3".
  6. Product is installed at "/opt/ibm/db2/V9.1".
复制代码
准备:

  1. /tmp> db2 "create table x (x int not null primary key)"
  2. DB20000I  The SQL command completed successfully.
  3. /tmp> db2 "insert into x values (1)"
  4. DB20000I  The SQL command completed successfully.
  5. /tmp> db2 "insert into x values (2)"
  6. DB20000I  The SQL command completed successfully.
  7. /tmp> db2 "insert into x values (3)"
  8. DB20000I  The SQL command completed successfully.
  9. /tmp> db2 "insert into x values (4)"
  10. DB20000I  The SQL command completed successfully.
  11. /tmp> db2 "insert into x values (5)"
  12. DB20000I  The SQL command completed successfully.
  13. /tmp> db2 "insert into x values (6)"
  14. DB20000I  The SQL command completed successfully.
  15. /tmp> db2 "insert into x values (7)"
  16. DB20000I  The SQL command completed successfully.
  17. /tmp> db2 "select * from  x"

  18. X         
  19. -----------
  20.           1
  21.           2
  22.           3
  23.           4
  24.           5
  25.           6
  26.           7

  27.   7 record(s) selected.
复制代码
启动三个窗口。依次进行如下操作。
窗口1:

  1. /tmp> db2 +c +p
  2. lock table x in share mode
  3. DB20000I  The SQL command completed successfully.
复制代码
窗口2:

  1. /tmp> db2 +c +p
  2. lock table x in exclusive mode
复制代码
此时,窗口2执行语句被阻塞

窗口3:

  1. /home/fex> db2 "select x from x where x = 1"

复制代码
此时,窗口3的语句也被阻塞。


问题:窗口2语句显然没有能够锁定表x,为何窗口3语句还会被共享锁阻塞?

如果您要回复我窗口2语句没有完成,但是仍然持有了一个internal varriable lock。
我的主要疑问是:数据库使用何种方法来保证acid,从使用者角度来说,应该是不用关心的。
问题在于,在“原子性”的约束下,窗口2的执行应该是“要么排他锁定x,要么什么都不锁定”。
对于出现上述现象,我是不是可以理解成DB2的一个BUG?
作者: 3sane    时间: 2011-03-23 16:08
回复 1# ivhb


    窗口2持有了IX锁,把他终止掉看看?
作者: ivhb    时间: 2011-03-26 00:33
回复 2# 3sane
1,我查过的,没有IX锁(您说的应该是表上的吧)
2,即便是IX,也不应该阻止我访问其中的一条记录。
作者: ivhb    时间: 2011-03-29 12:39
谁给我个解释?
作者: snow888    时间: 2011-04-16 13:49
俺遇到的问题是,db2 根本就没有锁机制,俺实在不知道如何实现行级锁。。。。

按照书上的方法(包括您这个例子),俺都试过了,没发现能实现锁。

俺的  db2 是 v9.7 的。
作者: mdkii    时间: 2011-04-28 11:09
本帖最后由 mdkii 于 2011-04-28 11:15 编辑

LZ的问题应该是db2 锁的排队机制造成的(避免活锁的情况发生)。
LS的采用的db2v9.7。在9.7中db2实现了Currently Committed(当前已落实)功能
因此select不会被block。
作者: ivhb    时间: 2011-04-29 10:47
LZ的问题应该是db2 锁的排队机制造成的(避免活锁的情况发生)。
LS的采用的db2v9.7。在9.7中db2实现了Cur ...
mdkii 发表于 2011-04-28 11:09


这个解释靠谱,
只是觉得排队机制下,已经对acid造成了破坏。简直颠覆了俺对数据库的认知。呵呵




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2