分类: DB2/Informix
2013-07-22 17:04:34
在网上看到了很多关于DB2死锁的模拟,无奈,由于版本原因,个人在DB2 9.7上,按照操作始终没有成功。后来经过别人指点,算是完成了死锁的模拟操作,下面将操作环节和注意事项记录如下:
1、查看数据库版本
$ db2level
DB21085I Instance "db2test" uses "64" bits and DB2 code release "SQL09075"
with level identifier "08060107".
Informational tokens are "DB2 v9.7.0.5", "special_28492", "IP23285_28492", and
Fix Pack "5".
Product is installed at "/db2/NIT/db2_software".
2、准备数据库表
db2 "create table t11 (col char(10))"
db2 "create table t21 (col char(10))"
3、修改数据库参数
注意,这步很重要,因为从9.7开始,DB2引入了Currently Commit机制,用于防止写操作阻碍读操作,减少锁等从而提高并发。
默认情况下,此参数是open的,所以,关闭之:
$ db2 update db cfg using CUR_COMMIT DISABLED
DB20000I The UPDATE DATABASE CONFIGURATION command completed successfully.
SQL1363W One or more of the parameters submitted for immediate modification
were not changed dynamically. For these configuration parameters, all
applications must disconnect from this database before the changes become
effective.
$
提示需要断开所有application后才能生效
4、开始进行死锁模拟
打开两个CLP窗口,分别连接到sample库,然后:
在CLP1中执行:
$ db2 +c "insert into t11 values('aaa')"
DB20000I The SQL command completed successfully.
$
在CLP2中执行:
$ db2 +c "insert into t21 values('bbb')"
DB20000I The SQL command completed successfully.
$
执行完插入操作后,在CLP1和CLP2中尽量同步执行以下查询:
在CLP1中执行:
$ db2 +c "select * from t21"
在CLP2中执行:
$ db2 +c "select * from t11"
不用多少时间,就会发现,CLP窗口中报错:
CLP2输出:
$ db2 +c "select * from t11"
SQL0911N The current transaction has been rolled back because of a deadlock
or timeout. Reason code "2". SQLSTATE=40001
$
CLP1输出:
$ db2 +c "select * from t21"
COL1
----------
ttt
1 record(s) selected.
已经出现了死锁,并且被死锁管理器选择一个application干掉了,解开死锁。
5、查看db2diag.log中是否有死锁信息:
奇怪的是,没有找到报错信息(死锁报错应该是911,reason code 2 才对),只是找到了db2cos的调用的event信息(因为开了db2cos的deadlock回调脚本),内容如下:
2013-07-22-10.45.46.925628+120 I17650A484 LEVEL: Event
PID : 16121922 TID : 9255 PROC : db2sysc 0
INSTANCE: db2test NODE : 000 DB : SAMPLE
APPHDL : 0-168 APPID: *LOCAL.db2test.130722083625
AUTHID : DB2TEST
EDUID : 9255 EDUNAME: db2agent (SAMPLE) 0
FUNCTION: DB2 UDB, trace services, pdInvokeCalloutScript, probe:10
START : Invoking /home/db2test/sqllib/adm/db2cos from lock manager sqlplnfd
2013-07-22-10.45.49.938213+120 I18135A541 LEVEL: Warning
PID : 16121922 TID : 9255 PROC : db2sysc 0
INSTANCE: db2test NODE : 000 DB : SAMPLE
APPHDL : 0-168 APPID: *LOCAL.db2test.130722083625
AUTHID : DB2TEST
EDUID : 9255 EDUNAME: db2agent (SAMPLE) 0
FUNCTION: DB2 UDB, trace services, pdInvokeCalloutScript, probe:110
MESSAGE : Detected end of execution of call-out script
DATA #1 : Process ID, 4 bytes
16777302
DATA #2 : unsigned integer, 4 bytes
3
2013-07-22-10.45.49.938442+120 I18677A467 LEVEL: Event
PID : 16121922 TID : 9255 PROC : db2sysc 0
INSTANCE: db2test NODE : 000 DB : SAMPLE
APPHDL : 0-168 APPID: *LOCAL.db2test.130722083625
AUTHID : DB2TEST
EDUID : 9255 EDUNAME: db2agent (SAMPLE) 0
FUNCTION: DB2 UDB, trace services, pdInvokeCalloutScript, probe:20
STOP : Completed invoking /home/db2test/sqllib/adm/db2cos
结论:在DB2 9.7上,Currently Committed机制是默认打开的,这个机制会影响到死锁的模拟(这可能是网上很多方案我无法模拟成功的原因)。但是,为什么没有在db2diag.log文件中发现死锁的报错,是我所不理解的,需要后续调查。