DB2 for Linux, UNIX, and Windows 的锁事件,第 3 部分: 使用 DB2 9.7 中的锁事件监控器来解决并发性问题
2010-08-03 00:00:00 来源:WEB开发网第二个会话中的 UPDATE 语句被第一个会话中的事务所持有的独占锁阻塞,因此它必须等待。现在在第一个会话中执行一个试图改变被第二个会话事务阻塞的行(E11 部门的行)的 UPDATE 语句。数据库识别出死锁事件。
清单 18. 第一个事务:更新另一个部门的位置信息
db2 +c "UPDATE DEPARTMENT SET LOCATION = 'Los Angeles' WHERE DEPTNO = 'E11'"
数据库管理器自动回滚其中一个事务,第一个会话中的或第二个会话中的。出错的事务的 UPDATE 语句返回相应错误信息,如清单 19 所示。
清单 19. DB2 返回的死锁错误
SQL0911N The current transaction has been rolled back because of a deadlock or timeout.
Reason code "2". SQLSTATE=40001
为了格式化锁事件监控器收集的死锁事件数据,并将其写到普通文本文件中,再次调用 db2evmonfmt 工具。由于第一个例子已编译过源代码,db2evmonfmt Java 类文件已存在。这一次,为限制工具输出死锁事件,将附加选项 -type 的值设置为 deadlock,如清单 20 所示。
清单 20. 调用 db2evmonfmt 工具来格式化为死锁事件收集的数据
java db2evmonfmt -d sample -ue emdata.tab_locking -type deadlock -ftext
-u username -p password >deadlock.txt
db2evmonfmt 的输出格式几乎与第一个例子中所描述的格式一致。根据应用程序、锁和 SQL 语句有关信息,可以轻松确定死锁事件的原因,如清单 21 所示。
清单 21. 死锁事件的 db2evmonfmt 输出
-------------------------------------------------------
Event ID : 1
Event Type : DEADLOCK
Event Timestamp : 2010-02-21-19.25.32.723805
Partition of detection : 0
-------------------------------------------------------
Deadlock Graph
--------------
Total number of deadlock participants : 2
Participant that was rolled back : 2
Type of deadlock : local
Participant Participant Deadlock Member Application Handle
Requesting Lock Holding Lock
--------------- --------------- --------------- ------------------
1 2 0 044
2 1 0 024
Participant No 1 requesting lock
----------------------------------
Lock Name : 0x020005000B0000000000000052
Lock wait start time : 2010-02-21-19.25.26.208252
Lock wait end time : 2010-02-21-19.25.32.723805
Lock Type : ROW
Lock Specifics : ROWID=11,DATA_PARTITION_ID=0,PAGEID=0
Lock Attributes : 00000000
Lock mode requested : Exclusive
Lock mode held : Exclusive
Lock Count : 1
Lock Hold Count : 0
Lock rrIID : 0
Lock Status : Waiting
Lock release flags : 40000000
Tablespace TID : 2
Tablespace Name : USERSPACE1
Table FID : 5
Table Schema : FECHNER
Table Name : DEPARTMENT
Participant No 2 requesting lock
----------------------------------
Lock Name : 0x02000500050000000000000052
Lock wait start time : 2010-02-21-19.25.13.381517
Lock wait end time : 2010-02-21-19.25.32.723805
Lock Type : ROW
Lock Specifics : ROWID=5,DATA_PARTITION_ID=0,PAGEID=0
Lock Attributes : 00000000
Lock mode requested : Exclusive
Lock mode held : Exclusive
Lock Count : 1
Lock Hold Count : 0
Lock rrIID : 0
Lock Status : Waiting
Lock release flags : 40000000
Tablespace TID : 2
Tablespace Name : USERSPACE1
Table FID : 5
Table Schema : FECHNER
Table Name : DEPARTMENT
Attributes Requester Requester
--------------------- ------------------------------ ------------------------------
Participant No 1 2
Application Handle 024 044
Application ID *LOCAL.DB2.100221182012 *LOCAL.DB2.100221182513
Application Name db2bp.exe db2bp.exe
Authentication ID FECHNER FECHNER
Requesting AgentID 4148 1016
Coordinating AgentID 4148 1016
Agent Status UOW Executing UOW Executing
Application Action No action No action
Lock timeout value 0 0
Lock wait value 0 0
Workload ID 1 1
Workload Name SYSDEFAULTUSERWORKLOAD SYSDEFAULTUSERWORKLOAD
Service subclass ID 13 13
Service subclass SYSDEFAULTSUBCLASS SYSDEFAULTSUBCLASS
Current Request Execute Immediate Execute Immediate
TEntry state 2 2
TEntry flags1 00000000 00000000
TEntry flags2 00000200 00000200
Lock escalation no no
Client userid
Client wrkstnname
Client applname
Client acctng
Current Activities of Participant No 1
----------------------------------------
Activity ID : 2
Uow ID : 4
Package Name : SQLC2H20
Package Schema : NULLID
Package Version :
Package Token : AAAAAZBZ
Package Sectno : 203
Reopt value : none
Incremental Bind : no
Eff isolation : CS
Eff degree : 0
Eff locktimeout : -1
Stmt unicode : no
Stmt query ID : 0
Stmt nesting level : 0
Stmt invocation ID : 0
Stmt source ID : 0
Stmt pkgcache ID : 631360192513
Stmt type : Dynamic
Stmt operation : DML, Insert/Update/Delete
Stmt text : UPDATE DEPARTMENT SET LOCATION = 'Los Angeles' WHERE DEPTNO = 'E11'
Past Activities of Participant No 1
-------------------------------------
Past Activities wrapped: no
Activity ID : 1
Uow ID : 4
Package Name : SQLC2H20
Package Schema : NULLID
Package Version :
Package Token : AAAAAZBZ
Package Sectno : 203
Reopt value : none
Incremental Bind : no
Eff isolation : CS
Eff degree : 0
Eff locktimeout : -1
Stmt unicode : no
Stmt query ID : 0
Stmt nesting level : 0
Stmt invocation ID : 0
Stmt source ID : 0
Stmt pkgcache ID : 2791728742402
Stmt type : Dynamic
Stmt operation : DML, Insert/Update/Delete
Stmt text : UPDATE DEPARTMENT SET LOCATION = 'New York' WHERE DEPTNO = 'B01'
Current Activities of Participant No 2
----------------------------------------
Activity ID : 2
Uow ID : 1
Package Name : SQLC2H20
Package Schema : NULLID
Package Version :
Package Token : AAAAAZBZ
Package Sectno : 203
Reopt value : none
Incremental Bind : no
Eff isolation : CS
Eff degree : 0
Eff locktimeout : -1
Stmt unicode : no
Stmt query ID : 0
Stmt nesting level : 0
Stmt invocation ID : 0
Stmt source ID : 0
Stmt pkgcache ID : 4380866641921
Stmt type : Dynamic
Stmt operation : DML, Insert/Update/Delete
Stmt text : UPDATE DEPARTMENT SET DEPTNAME = 'PLANNING 1' WHERE DEPTNO = 'B01'
Past Activities of Participant No 2
-------------------------------------
Past Activities wrapped: no
Activity ID : 1
Uow ID : 1
Package Name : SQLC2H20
Package Schema : NULLID
Package Version :
Package Token : AAAAAZBZ
Package Sectno : 203
Reopt value : none
Incremental Bind : no
Eff isolation : CS
Eff degree : 0
Eff locktimeout : -1
Stmt unicode : no
Stmt query ID : 0
Stmt nesting level : 0
Stmt invocation ID : 0
Stmt source ID : 0
Stmt pkgcache ID : 3173980831746
Stmt type : Dynamic
Stmt operation : DML, Insert/Update/Delete
Stmt text : UPDATE DEPARTMENT SET DEPTNAME = 'OPERATIONS 1' WHERE DEPTNO = 'E11'
删除锁事件监控器
不再需要锁事件监控器时,可以将其停用并删除。如果需要锁事件监控器来分析可能的并发性问题,就只需将其停用。删除锁事件监控器不会隐式删除对应的非格式化事件表。如果不需要保留所收集的锁事件数据用作将来的分析,必须使用 DROP TABLE 语句手动删除非格式化事件表,如清单 22 所示。
清单 22. 删除锁事件监控器
db2 "SET EVENT MONITOR EVMON_LOCKING STATE 0"
db2 "DROP EVENT MONITOR EVMON_LOCKING"
db2 "DROP TABLE EMDATA.TAB_LOCKING"
结束语
本文介绍了如何使用 DB2 LUW 9.7 中引入的新的锁事件监控器。本文通过实例演示了如何使用锁事件监控器来收集所有种类的并发性问题:死锁、锁超时及锁等待。使用新的格式化工具 db2evmonfmt,收集的锁事件信息可以通过这种方式格式化,让确认某种锁问题的原因变得更简单。DB2 LUW 9.7 提供了一种集成方式来分析所有种类的并发性问题,它也取代了以前 DB2 LUW 发行版中使用的锁分析的方法。
更多精彩
赞助商链接