1. 程式人生 > >出現Lock-Wait鎖等待,怎麼解決

出現Lock-Wait鎖等待,怎麼解決

C:\>db2 update monitor switches using lock on  
C:\>db2 get snapshot for locks on datcmg | more 

Locks: 
Address            TranHdl    Lockname                   Type       Mode Sts Owner      Dur HoldCount  Att  ReleaseFlg
0x070000002F7B6B40 43         0002010C0000000077A3000252 Row        .NS  G   43         1   0          0x00 0x00000001 

0x07000000302241C0 23         0002010C0000000077A3000252 Row        ..X  W   43         1   0          0x00 0x40000000 
//事物23被事物43鎖了 

Transactions: 
Address            AppHandl [nod-index] TranHdl    Locks      State   Tflag      Tflag2     Firstlsn       Lastlsn        LogSpace        SpaceReserved   TID            AxRegCnt   GXID     ClientUserID                   ClientWrkstnName               ClientApplName                 ClientAccntng                  

0x0700000020326500 22334    [000-22334] 23         4          READ    0x00000000 0x00000000 0x000000000000 0x000000000000 0               0               0x000000A1B3E7 1          0        n/a                            JYKL0                          n/a                            n/a                            
0x0700000020339B00 22322    [000-22322] 43         29         READ    0x00000000 0x00000000 0x000000000000 0x000000000000 0               0               0x000000A1AFE5 1          0        n/a                            JYKL0                          n/a                            n/a                            

//事物23關聯應用22334,事物43關聯應用22322 

Applications: 
Address            AppHandl [nod-index] NumAgents  CoorEDUID  Status                  C-AnchID C-StmtUID  L-AnchID L-StmtUID  Appid         
0x0780000001070080 22334    [000-22334] 1          26791      Lock-wait               53       5393       243      7524       182.100.100.50.51075.1002010812      
0x0780000001203260 22322    [000-22322] 1          6427       UOW-Waiting             0        0          188      7426       182.100.100.50.51023.1002010812   
//通過sql座標可以定位鎖sql和被鎖sql 

Dynamic SQL Statements: 
Address            AnchID StmtUID    NumEnv     NumVar     NumRef     NumExe     Text 
0x070000002F019680 53     5393       1          1          13         13         DELETE FROM LJSGetDraw WHERE 
    GetNoticeNo = ? AND PolNo = ? AND DutyCode = ? AND GetDutyKind = ? AND GetDutyCode = ? AND Currency = ? 
0x070000003A28CBE0 188    7426       1          1          1          1          SELECT * FROM LJSGetDraw a 
    WHERE not exists(select 'X' from LJAGetDraw where PolNo=a.PolNo and DutyCode=a.DutyCode and GetDutyCode=a.GetDutyCode and 
    GetDutyKind=a.GetDutyKind and GetNoticeNo=a.GetNoticeNo) and GetDate<=date('2010-01-29') and nvl(ComeFlag,'x')<>'1' and 
    RReportFlag='0' and (not exists(select 1 from lccontstate where PolNo=a.PolNo and statetype='Available' and state='1' and 
    enddate is null) and not exists(select 'X' from lcconthangupstate where contno =a.contno and hanguptype='2' ) and 
    exists(select 'X' from LCPol where PolNo=a.PolNo and AppFlag='1' ) or exists (select 'x' from LMDutyGetAlive where 
    MAXGETCOUNTTYPE='0' and GETDUTYCODE=a.GETDUTYCODE)) 
//WebSphere的jndi連線池自動設定高級別的db2隔離級別 

不同一個數據庫連線的事物會有隔離級別的控制,在本地連線db2級別低,但是WebSphere的jndi連線池自動設定高級別的db2隔離級別, 
所以在長時間使用sql語句的時候,需要手工設定隔離級別為最低ur,來避免鎖。 
具體修改是在需要進行操作的sql語句後面加上事物的級別
eg:db2 select * from User with ur,對於不是使用sql語句的寫法可以改成JDBC操作。
所有sql都手工設定隔離級別ur 

死鎖監控器: 
9.建立一個針對死鎖的event monitor 
db2 "create event monitor dlmon for deadlocks with details write to file '/home/db2inst1/evmon'"; 
db2 set event monitor dlmon state 1;啟用 
db2evmon -db datcmg -evm dlmon >/home/db2inst1/檔名 

sql語句效能建議: 
(請注意用相應db2使用者telnet) 
db2adivs -d dbname -i 1.sql>/home/db2inst1/檔名 

抓快照: 
db2 get snapshot for db on datcmg > /home/db2inst1/檔名 

sql語句慢的快照: 
db2 get snapshot for dynamic sql on sample 

查詢執行慢的sql: 
select * from sysibmadm.top_dynamic_sql order by average_execution_time_s desc