db2_查詢鎖方法

redhouser發表於2011-08-03

1,獲取鎖方法:
1.1管理檢視:
SELECT * FROM SYSIBMADM.SNAPLOCK
select * from SYSIBMADM.LOCKS_HELD

1.2 表函式:
SELECT * FROM TABLE(SNAP_GET_LOCK('SAMPLE',-1)) AS SNAPLOCK;
SELECT * FROM TABLE(SNAPSHOT_LOCK('SAMPLE',-1)) AS SNAPLOCK;

1.3 CLP:
get snapshot for locks for application applid appl-id
get snapshot for locks for application agentid appl-handle
get snapshot for locks on dbname

1.4 db2bp工具:
db2pd -inst db2 -db sample -locks

2,案例
2.0 環境準備
--db2 connect to sample
--db2 get dbm monitor switches
     收集到的 DBM 系統監視器資訊

資料庫分割槽號 0 的開關列表
緩衝池活動資訊        (BUFFERPOOL) = OFF
鎖定資訊                    (LOCK) = ON  2011-08-03 10:53:09.067114
排序資訊                    (SORT) = OFF
QL 語句資訊           (STATEMENT) = OFF
表活動資訊                 (TABLE) = OFF
獲取時間戳記資訊(時間戳記)    = ON  2011-08-03 10:51:55.773736
工作單元資訊                 (UOW) = OFF

--db2 get monitor switches
            監視器記錄開關
資料庫分割槽號 0 的開關列表
緩衝池活動資訊        (BUFFERPOOL) = OFF
鎖定資訊                    (LOCK) = OFF
排序資訊                    (SORT) = OFF
SQL 語句資訊           (STATEMENT) = OFF
表活動資訊                 (TABLE) = OFF
獲取時間戳記資訊(時間戳記)    = ON  2011-08-03 10:51:55.773736
工作單元資訊                 (UOW) = OFF

--db2 update monitor switches using LOCK on
DB20000I  UPDATE MONITOR SWITCHES 命令成功完成。

--db2 get monitor switches
            監視器記錄開關
資料庫分割槽號 0 的開關列表
緩衝池活動資訊        (BUFFERPOOL) = OFF
鎖定資訊                    (LOCK) = ON  2011-08-03 13:57:43.085020
排序資訊                    (SORT) = OFF
SQL 語句資訊           (STATEMENT) = OFF
表活動資訊                 (TABLE) = OFF
獲取時間戳記資訊(時間戳記)    = ON  2011-08-03 10:51:55.773736
工作單元資訊                 (UOW) = OFF

--db2 -c- declare c1 cursor for select * from staff where job='Sales' for update
DB20000I  SQL 命令成功完成。

--db2 -c- open c1
DB20000I  SQL 命令成功完成。

--db2 -c- fetch c1

ID     NAME      DEPT   JOB   YEARS  SALARY    COMM
------ --------- ------ ----- ------ --------- ---------
    20 Pernal        20 Sales      8  78171.25    612.45

  1 條記錄已選擇。


2.1透過管理檢視查詢
--db2 select * from SYSIBMADM.LOCKS_HELD
SNAPSHOT_TIMESTAMP         DB_NAME  AGENT_ID     APPL_NAME  AUTHID 
-------------------------- -------- ------------ ---------- -------
2011-08-03-16.20.17.373920 SAMPLE            300 db2bp.exe  mh 
2011-08-03-16.20.17.373920 SAMPLE            300 db2bp.exe  mh 
2011-08-03-16.20.17.373920 SAMPLE            300 db2bp.exe  mh 
2011-08-03-16.20.17.373920 SAMPLE            300 db2bp.exe  mh 
2011-08-03-16.20.17.373920 SAMPLE            300 db2bp.exe  mh 


TBSP_NAME  TABSCHEMA   TABNAME  TAB_FILE_ID    LOCK_OBJECT_TYPE  
---------- ----------- -------- -------------- ------------------
-          -           -                     0 INTERNALV_LOCK    
USERSPACE1 mh      STAFF                15 ROW_LOCK          
-          -           -                     0 INTERNALV_LOCK    
-          -           -                     0 INTERNALP_LOCK    
USERSPACE1 mh      STAFF                15 TABLE_LOCK        


LOCK_NAME                        LOCK_MODE  LOCK_STATUS LOCK_ESCALATION DBPARTITIONNUM
-------------------------------- ---------- ----------- --------------- --------------
0x01000000010000000100A02256     S          GRNT                      0              0
0x02000F00050000000000000052     U          GRNT                      0              0
0x02000000010000000100204056     S          GRNT                      0              0
0x53514C43324832307F4760B841     S          GRNT                      0              0
0x02000F00000000000000000054     IX         GRNT                      0              0


--db2 SELECT * FROM SYSIBMADM.SNAPLOCK
SNAPSHOT_TIMESTAMP         AGENT_ID TAB_FILE_ID  LOCK_OBJECT_TYPE   LOCK_MODE 
-------------------------- -------- ------------ ------------------ ----------
2011-08-03-16.21.58.921579      300           0  INTERNALV_LOCK     S         
2011-08-03-16.21.58.921579      300           0  INTERNALP_LOCK     S         


LOCK_STATUS LOCK_ESCALATION TABNAME  TABSCHEMA   TBSP_NAME LOCK_ATTRIBUTES LOCK_COUNT
----------- --------------- -------- ----------- --------- --------------- ----------
GRNT                      0 -        -           -         NONE            1         
GRNT                      0 -        -           -         NONE            1         


LOCK_CURRENT_MODE LOCK_HOLD_COUNT  LOCK_NAME                        LOCK_RELEASE_FLAGS   
----------------- ---------------- -------------------------------- -------------------- 
-                                0 0x01000000020000000100207356               1073741824 
-                                0 0x53514C43324832307F4760B841               1073741824 


DATA_PARTITION_ID DBPARTITIONNUM   
----------------- --------------   
                -              0   
                -              0   


2.2使用表函式
--db2 SELECT * FROM TABLE(SNAP_GET_LOCK('SAMPLE',-1)) AS SNAPLOCK
SNAPSHOT_TIMESTAMP         AGENT_ID    TAB_FILE_ID   LOCK_OBJECT_TYPE  
-------------------------- ----------- ------------- ------------------
2011-08-03-16.47.14.996360         300             0 INTERNALV_LOCK    
2011-08-03-16.47.14.996360         300             0 INTERNALP_LOCK    

LOCK_MODE  LOCK_STATUS LOCK_ESCALATION TABNAME  
---------- ----------- --------------- ---------
S          GRNT                      0 -        
S          GRNT                      0 -        

TABSCHEMA  TBSP_NAME  LOCK_ATTRIBUTES  LOCK_COUNT 
---------- ---------- ---------------- -----------
-          -          NONE                       1
-          -          NONE                       1

LOCK_CURRENT_MODE LOCK_HOLD_COUNT      LOCK_NAME                       
----------------- -------------------- --------------------------------
-                                    0 0x01000000020000000100805E56    
-                                    0 0x53514C43324832307F4760B841    

LOCK_RELEASE_FLAGS   DATA_PARTITION_ID DBPARTITIONNUM
-------------------- ----------------- --------------
          1073741824                 -              0
          1073741824                 -              0

--db2 SELECT * FROM TABLE(SNAPSHOT_LOCK('SAMPLE',-1)) AS SNAPLOCK
SNAPSHOT_TIMESTAMP         AGENT_ID    TABLE_FILE_ID  LOCK_OBJECT_TYPE 
-------------------------- ----------- -------------- -----------------
2011-08-03-16.50.44.806587         300              0                 9
2011-08-03-16.50.44.806587         300              0                 8

LOCK_MODE  LOCK_STATUS   LOCK_OBJECT_NAME     PARTITION_NUMBER
---------- ------------- -------------------- ----------------
         3             1                    0                -
         3             1                    0                -

LOCK_ESCALATION TABLE_NAME TABLE_SCHEMA  TABLESPACE_NAME
--------------- ---------- ------------- ----------------
              0 -          -             -              
              0 -          -             -             

2.3CLP
--db2 get snapshot for locks on sample

            資料庫鎖定快照

資料庫名稱                               = SAMPLE
資料庫路徑                          = C:\DB2\NODE0000\SQL00001\
輸入資料庫別名                      = SAMPLE
掛起的鎖定                      = 16
當前已連線的應用程式            = 4
當前正等待鎖定的代理程式數      = 0
快照時間戳記                    = 2011-08-03 16:17:06.280072

...

應用程式控制程式碼                               = 300
應用程式標識                        = *LOCAL.DB2.110803055722
序號                                = 00006
應用程式名                          = db2bp.exe
CONNECT 授權標識                           = mh
應用程式狀態                        = UOW 正在等待
狀態更改時間                        = 未收集
應用程式內碼表                             = 1386
掛起的鎖定                      = 4
總計等待時間(毫秒)            = 0

鎖定列表
 鎖定名稱                       = 0x02000F00050000000000000052
 鎖定屬性                       = 0x00000000
 發行版標誌                     = 0x00000001
 鎖定計數                       = 1
 掛起計數                       = 0
 鎖定物件名                     = 5
 物件型別                       = 行
 表空間名                       = USERSPACE1
 表模式               = mh
 表名                 = STAFF
 方式                           = U

 鎖定名稱                       = 0x02000000010000000100204056
 鎖定屬性                       = 0x00000000
 發行版標誌                     = 0x40000000
 鎖定計數                       = 1
 掛起計數                       = 0
 鎖定物件名                     = 0
 物件型別                       = 內部變化鎖定
 方式                           = S

 鎖定名稱                       = 0x53514C43324832307F4760B841
 鎖定屬性                       = 0x00000000
 發行版標誌                     = 0x40000000
 鎖定計數                       = 1
 掛起計數                       = 0
 鎖定物件名                     = 0
 物件型別                       = 內部方案鎖定
 方式                           = S

 鎖定名稱                       = 0x02000F00000000000000000054
 鎖定屬性                       = 0x00000000
 發行版標誌                     = 0x00000001
 鎖定計數                       = 1
 掛起計數                       = 0
 鎖定物件名                     = 15
 物件型別                       = 表
 表空間名                       = USERSPACE1
 表模式               = mh
 表名                 = STAFF
 方式                           = IX

--db2 get snapshot for locks for application agentid 300

            應用程式鎖定快照

快照時間戳記                    = 2011-08-03 16:19:18.814303

應用程式控制程式碼                               = 300
應用程式標識                        = *LOCAL.DB2.110803055722
序號                                = 00006
應用程式名                          = db2bp.exe
CONNECT 授權標識                           = mh
應用程式狀態                        = UOW 正在等待
狀態更改時間                        = 未收集
應用程式內碼表                             = 1386
掛起的鎖定                      = 4
總計等待時間(毫秒)            = 0

鎖定列表
 鎖定名稱                       = 0x02000F00050000000000000052
 鎖定屬性                       = 0x00000000
 發行版標誌                     = 0x00000001
 鎖定計數                       = 1
 掛起計數                       = 0
 鎖定物件名                     = 5
 物件型別                       = 行
 表空間名                       = USERSPACE1
 表模式               = mh
 表名                 = STAFF
 方式                           = U

 鎖定名稱                       = 0x02000000010000000100204056
 鎖定屬性                       = 0x00000000
 發行版標誌                     = 0x40000000
 鎖定計數                       = 1
 掛起計數                       = 0
 鎖定物件名                     = 0
 物件型別                       = 內部變化鎖定
 方式                           = S

 鎖定名稱                       = 0x53514C43324832307F4760B841
 鎖定屬性                       = 0x00000000
 發行版標誌                     = 0x40000000
 鎖定計數                       = 1
 掛起計數                       = 0
 鎖定物件名                     = 0
 物件型別                       = 內部方案鎖定
 方式                           = S

 鎖定名稱                       = 0x02000F00000000000000000054
 鎖定屬性                       = 0x00000000
 發行版標誌                     = 0x00000001
 鎖定計數                       = 1
 掛起計數                       = 0
 鎖定物件名                     = 15
 物件型別                       = 表
 表空間名                       = USERSPACE1
 表模式               = mh
 表名                 = STAFF
 方式                           = IX


2.4 db2pd工具
--db2pd -inst db2 -db sample -locks
Database Partition 0 -- Database SAMPLE -- Active -- Up 0 days 05:38:35 
Locks:
Address    TranHdl Lockname                   Type  Mode Sts Owner Dur HoldCount Att        ReleaseFlg rrIID
0x7FC75200 8       02001C00000000000000000054 Table .IN  G   8     1   1         0x00002000 0x40000000 0    
0x7FC71A80 6       02001600000000000000000054 Table .IX  G   6     1   1         0x00003000 0x40000000 0    
0x7FC74300 9       02001D00000000000000000054 Table .IN  G   9     3   1         0x00002000 0x40000000 0    
0x7FC74C80 8       02001700000000000000000054 Table .IN  G   8     3   1         0x00002000 0x40000000 0    
0x7FC74400 9       02001E00000000000000000054 Table .IN  G   9     1   1         0x00002000 0x40000000 0    
0x7FC74D80 8       02001800000000000000000054 Table .IN  G   8     1   1         0x00002000 0x40000000 0    
0x7FC74E80 8       02001900000000000000000054 Table .IN  G   8     1   1         0x00002000 0x40000000 0    
0x7FC71B00 6       02001300000000000000000054 Table .IX  G   6     1   1         0x00003000 0x40000000 0    
0x7FC74F80 8       02001A00000000000000000054 Table .IN  G   8     1   1         0x00002000 0x40000000 0    
0x7FC71980 6       02001400000000000000000054 Table .IX  G   6     1   1         0x00003000 0x40000000 0    
0x7FC75100 8       02001B00000000000000000054 Table .IN  G   8     1   1         0x00002000 0x40000000 0    
0x7FC71A00 6       02001500000000000000000054 Table .IX  G   6     1   1         0x00003000 0x40000000 0    

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/18922393/viewspace-704004/,如需轉載,請註明出處,否則將追究法律責任。

相關文章