Skip to content

Commit 6933fdc

Browse files
committed
docs(notes): add 2025-03-24-db2-lock-monitor-en.md
1 parent 27e8b35 commit 6933fdc

File tree

1 file changed

+125
-0
lines changed

1 file changed

+125
-0
lines changed
+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
---
2+
title: Monitoring Lock Activity in Db2
3+
lang: en
4+
layout: post
5+
audio: false
6+
translated: false
7+
generated: true
8+
---
9+
10+
I'll expand on how to monitor lock activity in Db2 using various tools and commands.
11+
12+
### Using db2pd Command
13+
14+
The `db2pd` command is a non-intrusive diagnostic tool that provides real-time information:
15+
16+
```
17+
db2pd -locks detailed
18+
```
19+
20+
This provides detailed lock information including:
21+
- Lock holder details (application handle, authorization ID)
22+
- Lock wait details
23+
- Lock object type (table, row, etc.)
24+
- Lock state (share, exclusive, etc.)
25+
- Lock duration
26+
27+
For more specific lock information:
28+
```
29+
db2pd -transactions
30+
db2pd -applications
31+
db2pd -locks show detail
32+
```
33+
34+
### Using Administrative Views and Table Functions
35+
36+
#### SYSIBMADM.SNAPLOCK View
37+
```sql
38+
SELECT
39+
AGENT_ID,
40+
TABSCHEMA,
41+
TABNAME,
42+
LOCK_OBJECT_TYPE,
43+
LOCK_MODE,
44+
LOCK_STATUS,
45+
LOCK_ESCALATION
46+
FROM SYSIBMADM.SNAPLOCK
47+
WHERE TABNAME = 'YOUR_TABLE_NAME'
48+
```
49+
50+
#### MON_LOCKWAITS Table Function
51+
```sql
52+
SELECT
53+
APPLICATION_HANDLE,
54+
APPLICATION_NAME,
55+
LOCK_OBJECT_TYPE,
56+
LOCK_MODE,
57+
LOCK_WAIT_START_TIME,
58+
LOCK_WAIT_ELAPSED_TIME
59+
FROM TABLE(MON_LOCKWAITS(-2)) AS LOCKWAITS
60+
```
61+
62+
#### MON_GET_LOCKS Table Function
63+
```sql
64+
SELECT
65+
APPLICATION_HANDLE,
66+
LOCK_OBJECT_TYPE,
67+
LOCK_NAME,
68+
LOCK_MODE,
69+
LOCK_STATUS
70+
FROM TABLE(MON_GET_LOCKS(NULL, -2)) AS T
71+
```
72+
73+
### Using ADMIN_GET_TAB_INFO Table Function
74+
To see which applications are holding locks on specific tables:
75+
76+
```sql
77+
SELECT
78+
TABSCHEMA,
79+
TABNAME,
80+
LOCK_COUNT,
81+
LOCK_WAIT_COUNT,
82+
LOCK_WAIT_TIME,
83+
LOCK_ESCAL_COUNT
84+
FROM TABLE(ADMIN_GET_TAB_INFO('', '', -2)) AS T
85+
```
86+
87+
### Identify Locking Applications
88+
89+
```sql
90+
SELECT
91+
APPL_ID,
92+
AGENT_ID,
93+
APPL_NAME,
94+
AUTHID,
95+
LOCK_WAIT_TIME,
96+
LOCKS_WAITING
97+
FROM TABLE(MON_GET_CONNECTION(NULL, -2)) AS CONN
98+
WHERE LOCKS_WAITING > 0
99+
```
100+
101+
### Solving Lock Contention
102+
103+
After identifying the lock issues:
104+
105+
1. **Force applications if necessary**:
106+
```
107+
db2 force application (application-handle)
108+
```
109+
110+
2. **Adjust isolation levels** for problematic transactions:
111+
```sql
112+
SET CURRENT ISOLATION = CS; -- Cursor Stability
113+
```
114+
115+
3. **Modify application behavior**:
116+
- Reduce transaction duration
117+
- Use row-level locking instead of table-level
118+
- Add WITH UR to read-only queries
119+
120+
4. **Configure lock timeouts**:
121+
```sql
122+
UPDATE DB CFG FOR database_name USING LOCKTIMEOUT 30
123+
```
124+
125+
These monitoring techniques help identify lock contention issues in your Db2 database so you can resolve them effectively.

0 commit comments

Comments
 (0)