窗口1:
[db2inst1@t3-dtpoc-dtpoc-web04 ~]$ db2 +c "update employee1 set name='yo11' where id=1"
DB20000I The SQL command completed successfully.
[db2inst1@t3-dtpoc-dtpoc-web04 ~]$ db2 +c "update employee1 set name='yo11' where id=2"
DB20000I The SQL command completed successfully.
窗口2:
[db2inst1@t3-dtpoc-dtpoc-web04 ~]$ db2 +c "update employee1 set name='yo12' where id=2"
DB20000I The SQL command completed successfully.
[db2inst1@t3-dtpoc-dtpoc-web04 ~]$ db2 +c "update employee1 set name='yo12' where id=1"
窗口3:
[db2inst1@t3-dtpoc-dtpoc-web04 ~]$ db2pd -d test -wlocks
Database Member 0 -- Database TEST -- Active -- Up 0 days 00:08:00 -- Date 2023-07-26-14.06.29.121734
Locks being waited on :
AppHandl [nod-index] TranHdl Lockname Type Mode Conv Sts CoorEDU AppName AuthID AppID
10407 [000-10407] 14 02000101050000000000000052 RowLock ..X G 453 db2bp DB2INST1 *LOCAL.db2inst1.230726055855
10395 [000-10395] 3 02000101050000000000000052 RowLock ..X W 423 db2bp DB2INST1 *LOCAL.db2inst1.230726055829
10395 [000-10395] 3 02000101040000000000000052 RowLock ..X G 423 db2bp DB2INST1 *LOCAL.db2inst1.230726055829
10407 [000-10407] 14 02000101040000000000000052 RowLock ..X W 453 db2bp DB2INST1 *LOCAL.db2inst1.230726055855
DB2会有死锁监控监制,检测到死锁会自动把其中一个应用回滚。
DB21034E The command was processed as an SQL statement because it was not a
valid Command Line Processor command. During SQL processing it returned:
SQL0911N The current transaction has been rolled back because of a deadlock
or timeout. Reason code "2". SQLSTATE=40001
如何查看死锁的具体信息呢?
DB2中有一个默认的deadlock event monitor:DB2DETAILDEADLOCK,通过db2 "select * from SYSCAT.EVENTMONITORS"可以看到
db2 set event monitor db2detaildeadlock state=0 //打开死锁监视器
db2 set event monitor db2detaildeadlock state=1 //关闭死锁监视器
会把死锁数据生成到数据库目录:
/home/db2inst1/db2inst1/NODE0000/SQL00001/MEMBER0000/db2event/db2detaildeadlock
[db2inst1@t3-dtpoc-dtpoc-web04 db2detaildeadlock]$ ls -ltr
total 12
-rw-r----- 1 db2inst1 db2inst1 43 Jul 17 10:05 db2event.ctl
-rw-r--r-- 1 db2inst1 db2inst1 5950 Jul 26 14:06 00000000.evt
[db2inst1@t3-dtpoc-dtpoc-web04 db2detaildeadlock]$
到这个目录下执行db2evmon -path . > deadlock.out
db2evmon -path . -file 24 > deadlock.out //指定文件