Skip to content

Instantly share code, notes, and snippets.

@angoca
Last active November 23, 2016 14:23
Show Gist options
  • Save angoca/0929d6c189f83cdd8b12c98389bc6f96 to your computer and use it in GitHub Desktop.
Save angoca/0929d6c189f83cdd8b12c98389bc6f96 to your computer and use it in GitHub Desktop.
Recreates a DB2 deadlock
#!/bin/bash
DATABASE=sample
TABLE1=t1
TABLE2=t2
db2 connect to ${DATABASE} > /dev/null
db2 "drop table ${TABLE1}" > /dev/null
db2 "drop table ${TABLE2}" > /dev/null
db2 "create table ${TABLE1} (col1 int, col2 varchar(15))"
db2 "create table ${TABLE2} (col1 int, col2 varchar(15))"
db2 "insert into ${TABLE1} values (1,'india'),(2,'usa'), (3,'russia')"
db2 "insert into ${TABLE2} values (1,'india'),(2,'usa'), (3,'russia')"
FILE=/tmp/deadlock.file
rm -v ${FILE}
# Creates the first session
(
db2 connect to ${DATABASE} > /dev/null
db2 -v +c "update ${TABLE1} set col2 = 'canada'"
echo "Session 1: One resource reserved"
CONT=true
while [[ ${CONT} ]] ; do
sleep 1
if [[ -r ${FILE} ]] ; then
echo "Session 1: File found"
break
else
echo "Session 1: Waiting for file"
fi
done
db2 -v +c "update ${TABLE2} set col2 = 'newyork'"
echo "$(date +"%Y-%m-%d %H:%M:%S") End session 1"
exit
) &
echo "$(date +"%Y-%m-%d %H:%M:%S") First session has been started and it is waiting"
# Creates the second session
(
db2 connect to ${DATABASE} > /dev/null
db2 -v +c "update ${TABLE2} set col2 = 'canada'"
echo "Session 2: One resource reserved"
CONT=true
while [[ ${CONT} ]] ; do
sleep 1
if [[ -r ${FILE} ]] ; then
echo "Session 2: File found"
break
else
echo "Session 2: Waiting for file"
fi
done
db2 -v +c "update ${TABLE1} set col2 = 'amazon'"
echo "$(date +"%Y-%m-%d %H:%M:%S") End session 2"
exit
) &
echo "$(date +"%Y-%m-%d %H:%M:%S") Second session has been started and it is waiting"
sleep 4
touch ${FILE}
echo "$(date +"%Y-%m-%d %H:%M:%S") Deadlock will occur soon"
@angoca
Copy link
Author

angoca commented Nov 22, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment