A classic version of shell scripts for solving deadlocks in DB2 databases!

Source: Internet
Author: User
Tags db2 connect

The following articles mainly describe the shell script for solving deadlocks in the DB2 database in linux/unix, A detailed description of the shell script used to unlock a deadlock in the DB2 database. I hope it will be helpful in your future studies.

Clear;

 
 
  1. DB2 "connect to db user dbuser using dbpass"  
  2. DB2 "update monitor switches using lock on table on"  
  3. tmpf=`echo "$HOME/tmp/""$RANDOM"_"$SECONDS"".lock"`  
  4. DB2 get snapshot for locks on abbs>$tmpf  
  5. handles=`cat $tmpf|awk '{if($2=="handle")print $4}'`  
  6. x=0;  
  7. y=0;  
  8. deadhandles=" " 
  9. for i in $handles  
  10. do  
  11.  

Obtains the row where the current id is located.

 
 
  1. ibeg=`cat $tmpf|awk '{if($2=="handle" && $4=="'$i'")print NR}'`;  
  2. #echo "Line[15]:handle:$i,beg:$ibeg" 

Retrieve content after the current row

 
 
  1. cat $tmpf|awk '{if(NR-1>'$ibeg')printf("%s\n", $0);}'>$tmpf.$i; 

Obtains the row where the next handle is located.

 
 
  1. hangdleaf=`cat $tmpf.$i|awk '{if($2=="handle")print NR}'`  
  2. #echo Line[20]:$hangdleaf  
  3. if [ -z "$hangdleaf" ];then  
  4. cat $tmpf.$i>$tmpf.$i.aft;  
  5. else 

Take the next handle

 
 
  1. itmp="";  
  2. for j in $hangdleaf  
  3. do  
  4. if [ -z "$itmp" ];then  
  5. handlenext=$j;  
  6. itmp="get done!" 
  7. fi  
  8. done  
  9. #echo Line[33]:$handlenext;  
  10. #cat $tmpf.$i|awk '{if(NR<'$handlenext')print $0;}'  
  11. cat $tmpf.$i|awk '{if(NR<'$handlenext')print $0;}'>$tmpf.$i.aft;  
  12. fi 

Determine whether the handle lock exists.

 
 
  1. Ifx = 'cat $ tmpf. $ I. aft | awk '{if ($1 = "Mode" & ($3 = "X" | $3 = "IX ")) | ($2 = "mode" & substr ($6, 2, 1) = "X") print $0 ;}''
  2. # Echo Line [39]: handle [$ I], $ ifx
  3. If [-z "$ ifx"]; then
  4. A = 1
  5. Else
  6. Deadhandles = "$ deadhandles" "$ I"
  7. X = 'expr $ x + 1 ';
  8. Echo $ I
  9. Fi
  10. # Cat $ tmpf. $ I. aft | grep X | more
  11. # Echo Line [48]: $ deadhandles
  12. # Read yn
  13. Rm-f $ tmpf. $ I
  14. Rm-f $ tmpf. $ I. aft
  15. Done
  16. # Echo "deadlock $ x: $ deadhandles ";
  17. # Read yn
  18. Clear
  19. If [-z "$ deadhandles"] | ["$ deadhandles" = ""]; then
  20. Echo "no deadlock, quit ......";
  21. Read yn;
  22. Exit 0;
  23. Fi
  24. Echo "Lock handle: $ deadhandles, unlock? Y/N \ c"
  25. Read yn;
  26. If [-z "$ yn"]; then
  27. Echo "Give up unlocking! "
  28. Exit 0;
  29. Fi
  30. If ["$ yn" = "Y"] | ["$ yn" = "y"]; then
  31. X = 0;
  32. For I in $ deadhandles
  33. Do
  34. Echo deadlock solution: $ I
  35. DB2 "force applications ($ I )"
  36. X = 'expr $ x + 1'
  37. Done
  38. Echo "unlock [$ x] locks ...."
  39. Else
  40. Echo "Give up unlocking! "
  41. Exit 0;
  42. Fi
  43. DB2 connect reset;
  44. DB2 terminate;
  45. Rm-f $ tmpf
  46. Echo "unlock terminal:">/tmp/tty. tmp
  47. Tty>/tmp/tty. tmp
  48. Wall </tmp/tty. tmp
  49. Rm-f/tmp/tty. tmp

The above content is an introduction to the shell script for resolving deadlocks in the DB2 database in linux/unix. I hope you will get some benefits.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.