Upload
raymartomampo
View
219
Download
0
Embed Size (px)
Citation preview
8/4/2019 1.5 - Data Con Currency and LEC
1/77
1 2011 IBM Corporation
Data concurrency and locking
IBM Information Management Cloud Computing Center of CompetenceIBM Canada Labs
8/4/2019 1.5 - Data Con Currency and LEC
2/77
2 2011 IBM Corporation
Transactions
Concurrency & Locking Lock Wait
Deadlocks
Agenda
8/4/2019 1.5 - Data Con Currency and LEC
3/77
3 2011 IBM Corporation
Reading materials
Getting started with DB2 Express-C eBook
Chapter 13: Concurrency and locking
Videos db2university.com course AA001EN
Lesson 8: Data concurrency and locking
Supporting reading material & videos
8/4/2019 1.5 - Data Con Currency and LEC
4/77
4 2011 IBM Corporation
Transactions
Concurrency & Locking Lock Wait
Deadlocks
Agenda
8/4/2019 1.5 - Data Con Currency and LEC
5/77
5 2011 IBM Corporation
What is a transaction?
Your bank account Your Moms bank account
Balance = $1000 Balance = $200
Transfer $100 from your account to your Moms account:
- Debit $100 from your bank account (Subtract $100)
- Credit $100 to your mom's bank account (Add $100)
8/4/2019 1.5 - Data Con Currency and LEC
6/776 2011 IBM Corporation
What is a transaction? (cont'd)
One or more SQL statements altogether treated as one
single unit
Also known as a Unit of Work (UOW)
A transaction starts with any SQL statement and ends with
a COMMIT or ROLLBACK COMMIT statement makes changes permanent to the
database
ROLLBACK statement reverses changes
COMMIT and ROLLBACK statements release all locks
8/4/2019 1.5 - Data Con Currency and LEC
7/777 2011 IBM Corporation
Example of transactions
INSERT INTO employee VALUES (100, 'JOHN')
INSERT INTO employee VALUES (200, 'MANDY')COMMIT
DELETE FROM employee WHERE name='MANDY'
UPDATE employee SET empID=101 where name='JOHN'
ROLLBACK
UPDATE employee SET name='JACK' where empID=100
COMMIT
ROLLBACK
First SQL statementstarts transaction
No changesapplied due to
ROLLBACK
There is nothing torollback
8/4/2019 1.5 - Data Con Currency and LEC
8/778 2011 IBM Corporation
Transactions ACID rules
Atomicity
All statements in the transaction are treated as a unit.
If the transaction completes successfully, everything is committed
If the transaction fails, everything done up to the point of failure is rolled back.
Consistency
Any transaction will take the data from one consistent state to another, so only
valid consistent data is stored in the database
Isolation Concurrent transactions cannot interfere with each other
Durability
Committed transactions have their changes persisted in the database
8/4/2019 1.5 - Data Con Currency and LEC
9/779 2011 IBM Corporation
Transactions
Concurrency & Locking
Lock Wait
Deadlocks
Agenda
8/4/2019 1.5 - Data Con Currency and LEC
10/7710 2011 IBM Corporation
Concurrency and Locking
ID Name Age
3 Peter 33
5 John 23
22 Mary 22
35 Ann 55
App A
App B
App C
App D
Concurrency:
Multiple users accessing the same resources at thesame time
Locking:
Mechanism to ensure data integrity and consistency
8/4/2019 1.5 - Data Con Currency and LEC
11/7711 2011 IBM Corporation
Locking
Locks are acquired automatically as needed to support a
transaction based on isolation levels
COMMIT and ROLLBACK statements release all locks
Two basic types of locks:
Share locks (S locks) acquired when an application wants to
read and prevent others from updating the same row
Exclusive locks (X locks) acquired when an application
updates, inserts, or deletes a row
8/4/2019 1.5 - Data Con Currency and LEC
12/77
8/4/2019 1.5 - Data Con Currency and LEC
13/7713 2011 IBM Corporation
Lost update
seat name ...
7C _____ 7B _____
...
reservations
App A App B
8/4/2019 1.5 - Data Con Currency and LEC
14/7714 2011 IBM Corporation
Lost update
update reservations
set name = 'John'
where seat = '7C'
seat name ...
7C _____ 7B _____
...
reservations
App A App B
8/4/2019 1.5 - Data Con Currency and LEC
15/7715 2011 IBM Corporation
Lost update
update reservations
set name = 'John'
where seat = '7C'
seat name ...
7C _____ 7B _____
...
reservations
John
App A App B
8/4/2019 1.5 - Data Con Currency and LEC
16/77
16 2011 IBM Corporation
Lost update
update reservations
set name = 'Mary'
where seat = '7C'
update reservations
set name = 'John'
where seat = '7C'
seat name ...
7C _____ 7B _____
...
reservations
John
App A App B
8/4/2019 1.5 - Data Con Currency and LEC
17/77
17 2011 IBM Corporation
Lost update
update reservations
set name = 'Mary'
where seat = '7C'
update reservations
set name = 'John'
where seat = '7C'
seat name ...
7C _____ 7B _____
...
reservations
JohnMary
App A App B
8/4/2019 1.5 - Data Con Currency and LEC
18/77
18 2011 IBM Corporation
Lost update
update reservations
set name = 'Mary'
where seat = '7C'
update reservations
set name = 'John'
where seat = '7C'
seat name ...
7C _____ 7B _____
...
reservations
JohnMary
App A App B
?
8/4/2019 1.5 - Data Con Currency and LEC
19/77
19 2011 IBM Corporation
Uncommitted read (also known as dirty read)
seat name ...
7C _____
7B _____
...
reservations
App A App B
8/4/2019 1.5 - Data Con Currency and LEC
20/77
20 2011 IBM Corporation
Uncommitted read (also known as dirty read)
seat name ...
7C _____
7B _____
...
reservations
update reservations
set name = 'John'
where seat = '7C'
App A App B
8/4/2019 1.5 - Data Con Currency and LEC
21/77
21 2011 IBM Corporation
Uncommitted read (also known as dirty read)
seat name ...
7C _____
7B _____
...
reservations
update reservations
set name = 'John'
where seat = '7C'
John
App A App B
8/4/2019 1.5 - Data Con Currency and LEC
22/77
22 2011 IBM Corporation
Uncommitted read (also known as dirty read)
seat name ...
7C _____
7B _____
...
reservations
update reservations
set name = 'John'
where seat = '7C'
John
App A App B
Select name
from reservations
where seat is '7C'
8/4/2019 1.5 - Data Con Currency and LEC
23/77
23 2011 IBM Corporation
Uncommitted read (also known as dirty read)
seat name ...
7C _____
7B _____
...
reservations
update reservations
set name = 'John'
where seat = '7C'
John
App A App B
Select name
from reservations
where seat is '7C'
John
8/4/2019 1.5 - Data Con Currency and LEC
24/77
24 2011 IBM Corporation
Uncommitted read (also known as dirty read)
seat name ...
7C _____
7B _____
...
reservations
update reservations
set name = 'John'
where seat = '7C'
John
App A App B
Select name
from reservations
where seat is '7C'
John
Roll back
8/4/2019 1.5 - Data Con Currency and LEC
25/77
25 2011 IBM Corporation
Uncommitted read (also known as dirty read)
seat name ...
7C _____
7B _____
...
reservations
update reservations
set name = 'John'
where seat = '7C'
App A App B
Select name
from reservations
where seat is '7C'
John
Roll back
8/4/2019 1.5 - Data Con Currency and LEC
26/77
26 2011 IBM Corporation
Uncommitted read (also known as dirty read)
John
Roll back
seat name ...
7C _____
7B _____
...
reservations
update reservations
set name = 'John'
where seat = '7C'
Select name
from reservations
where seat is '7C'
App A App B
Further processing in App
B uses incorrect /
uncommitted value of
John
Further processing in App
B uses incorrect /
uncommitted value of
John
8/4/2019 1.5 - Data Con Currency and LEC
27/77
27 2011 IBM Corporation
Non-repeatable read
seat name ...
7C _____
7B _____
...
reservations
App A App B
8/4/2019 1.5 - Data Con Currency and LEC
28/77
28 2011 IBM Corporation
Non-repeatable read
seat name ...
7C _____
7B _____
...
reservations
select seat
from reservations
where name is NULL
App A App B
8/4/2019 1.5 - Data Con Currency and LEC
29/77
29 2011 IBM Corporation
Non-repeatable read
seat name ...
7C _____
7B _____
...
reservations
select seat
from reservations
where name is NULL
App A App B
7C
7B
8/4/2019 1.5 - Data Con Currency and LEC
30/77
30 2011 IBM Corporation
Non-repeatable read
seat name ...
7C _____
7B _____
...
reservations
select seat
from reservations
where name is NULL
App A App B
7C
7B
update reservations
set name = 'John'
where seat = '7C'
8/4/2019 1.5 - Data Con Currency and LEC
31/77
31 2011 IBM Corporation
Non-repeatable read
seat name ...
7C _____
7B _____
...
reservations
select seat
from reservations
where name is NULL
App A App B
7C
7B
John
update reservations
set name = 'John'
where seat = '7C'
8/4/2019 1.5 - Data Con Currency and LEC
32/77
32 2011 IBM Corporation
Non-repeatable read
seat name ...
7C _____
7B _____
...
reservations
select seat
from reservations
where name is NULL
App A App B
7C
7B
John
update reservations
set name = 'John'
where seat = '7C'...
select seat
from reservations
where name is NULL
8/4/2019 1.5 - Data Con Currency and LEC
33/77
33 2011 IBM Corporation
Non-repeatable read
seat name ...
7C _____
7B _____
...
reservations
select seat
from reservations
where name is NULL
App A App B
7C
7B
John
update reservations
set name = 'John'
where seat = '7C'...
select seat
from reservations
where name is NULL7B
8/4/2019 1.5 - Data Con Currency and LEC
34/77
34 2011 IBM Corporation
Non-repeatable read
seat name ...
7C _____
7B _____
...
reservations
select seat
from reservations
where name is NULL
John
App A App B
update reservations
set name = 'John'
where seat = '7C'
7C
7B
...
select seat
from reservations
where name is NULL7B
The same SELECT (read) returns a
different result: Less rows (in this
case '7C' doesn't show anymore).
This is a non-repeatable read
The same SELECT (read) returns a
different result: Less rows (in this
case '7C' doesn't show anymore).
This is a non-repeatable read
8/4/2019 1.5 - Data Con Currency and LEC
35/77
35 2011 IBM Corporation
Phantom read
seat name ...
7C _____
7B _____
...
reservations
Susan
App A App B
8/4/2019 1.5 - Data Con Currency and LEC
36/77
36 2011 IBM Corporation
Phantom read
seat name ...
7C _____
7B _____
...
reservations
select seat
from reservations
where name is NULL
Susan
App A App B
8/4/2019 1.5 - Data Con Currency and LEC
37/77
37 2011 IBM Corporation
Phantom read
seat name ...
7C _____
7B _____
...
reservations
select seat
from reservations
where name is NULL
Susan
App A App B
7B
8/4/2019 1.5 - Data Con Currency and LEC
38/77
38 2011 IBM Corporation
Phantom read
seat name ...
7C _____
7B _____
...
reservations
select seat
from reservations
where name is NULL
Susan
App A App B
update reservations
set name = NULL
where seat = '7C'
7B
8/4/2019 1.5 - Data Con Currency and LEC
39/77
39 2011 IBM Corporation
Phantom read
seat name ...
7C _____
7B _____
...
reservations
select seat
from reservations
where name is NULL
App A App B
update reservations
set name = NULL
where seat = '7C'
7B
8/4/2019 1.5 - Data Con Currency and LEC
40/77
40 2011 IBM Corporation
Phantom read
seat name ...
7C _____
7B _____
...
reservations
select seat
from reservations
where name is NULL
App A App B
update reservations
set name = NULL
where seat = '7C'...
select seat
from reservations
where name is NULL
7B
8/4/2019 1.5 - Data Con Currency and LEC
41/77
41 2011 IBM Corporation
Phantom read
seat name ...
7C _____
7B _____
...
reservations
select seat
from reservations
where name is NULL
App A App B
update reservations
set name = NULL
where seat = '7C'
7C
7B
...
select seat
from reservations
where name is NULL
7B
8/4/2019 1.5 - Data Con Currency and LEC
42/77
42 2011 IBM Corporation
Phantom read
seat name ...
7C _____
7B _____
...
reservations
select seat
from reservations
where name is NULL
App A App B
update reservations
set name = NULL
where seat = '7C'
7C
7B
...
select seat
from reservations
where name is NULL
7B
The same SELECT (read) returns a
different result: More rows (phantom
rows, in this case '7C', is shown)
This is a phantom read
The same SELECT (read) returns a
different result: More rows (phantom
rows, in this case '7C', is shown)
This is a phantom read
8/4/2019 1.5 - Data Con Currency and LEC
43/77
43 2011 IBM Corporation
Isolation levels
Policies to control when locks are taken
DB2 provides different levels of protection to isolate data
Uncommitted Read (UR)
Cursor Stability (CS)
Currently committed (CC)
Read Stability (RS)
Repeatable Read (RR)
8/4/2019 1.5 - Data Con Currency and LEC
44/77
44 2011 IBM Corporation
Setting the isolation levels
Isolation level can be specified at many levels
Session (application),
Connection, Statement
For statement level, use the WITH {RR, RS, CS, UR} clause:
For embedded SQL, the level is set at bind time
For dynamic SQL, the level is set at run time
SELECT COUNT(*) FROM tab1 WITH UR
8/4/2019 1.5 - Data Con Currency and LEC
45/77
45 2011 IBM Corporation
Comparing isolation levels
8/4/2019 1.5 - Data Con Currency and LEC
46/77
46 2011 IBM Corporation
Cursor stability with currently committed (CC) semantics
Cursor stability with currently committedsemantics is the defaultisolation level
Use cur_commitdb cfg parameter to enable/disable Avoids timeouts and deadlocks
Cursorstability
Cursor stability with
currently committed
8/4/2019 1.5 - Data Con Currency and LEC
47/77
47 2011 IBM Corporation
Cursor stability with currently committed (CC) semantics
Cursor stability withoutcurrently committed
seat name ...
7C Susan7B _____
...
reservations
App A App B
Cursor stability withcurrently committed (Default behavior)
seat name ...7C Susan
7B _____
...
reservations
App A App B
8/4/2019 1.5 - Data Con Currency and LEC
48/77
48 2011 IBM Corporation
Cursor stability with currently committed (CC) semantics
Cursor stability withoutcurrently committed
seat name ...
7C Susan7B _____
...
reservations
App A App B
Cursor stability withcurrently committed (Default behavior)
seat name ...7C Susan
7B _____
...
reservations
App A App B
X
update
reservationsset name = 'John'where seat = '7C'
8/4/2019 1.5 - Data Con Currency and LEC
49/77
49 2011 IBM Corporation
Cursor stability with currently committed (CC) semantics
Cursor stability withoutcurrently committed
seat name ...
7C Susan7B _____
...
reservations
App A App B
Cursor stability withcurrently committed (Default behavior)
seat name ...7C Susan
7B _____
...
reservations
App A App B
X
update
reservationsset name = 'John'where seat = '7C'
John
8/4/2019 1.5 - Data Con Currency and LEC
50/77
50 2011 IBM Corporation
Cursor stability with currently committed (CC) semantics
Cursor stability withoutcurrently committed
seat name ...
7C Susan7B _____
...
reservations
App A App B
X
update
reservationsset name = 'John'where seat = '7C'
select namefrom reservations
where seat = '7C'S
Cursor stability withcurrently committed (Default behavior)
seat name ...7C Susan
7B _____
...
reservations
App A App B
John
8/4/2019 1.5 - Data Con Currency and LEC
51/77
51 2011 IBM Corporation
Cursor stability with currently committed (CC) semantics
Cursor stability withoutcurrently committed
seat name ...
7C Susan7B _____
...
reservations
App A App B
X
update
reservationsset name = 'John'where seat = '7C'
select namefrom reservations
where seat = '7C'S
Cursor stability withcurrently committed (Default behavior)
seat name ...7C Susan
7B _____
...
reservations
App A App B
John
LockWait
8/4/2019 1.5 - Data Con Currency and LEC
52/77
52 2011 IBM Corporation
Cursor stability with currently committed (CC) semantics
Cursor stability withoutcurrently committed
seat name ...
7C Susan7B _____
...
reservations
App A App B
X
update
reservationsset name = 'John'where seat = '7C'
select namefrom reservations
where seat = '7C'S
Cursor stability withcurrently committed (Default behavior)
seat name ...7C Susan
7B _____
...
reservations
App AApp B
John
LockWait
App B hangs until App A commits or rolls back which releases X lock
8/4/2019 1.5 - Data Con Currency and LEC
53/77
53 2011 IBM Corporation
Cursor stability with currently committed (CC) semantics
Cursor stability withoutcurrently committed
seat name ...
7C Susan7B _____
...
reservations
App A App B
X
update
reservationsset name = 'John'where seat = '7C'
select namefrom reservations
where seat = '7C'S
Cursor stability withcurrently committed (Default behavior)
seat name ...7C Susan
7B _____
...
reservations
App AApp B
John
LockWait
App B hangs until App A commits or rolls back which releases X lock
Xupdatereservations
set name = 'John'where seat = '7C'
8/4/2019 1.5 - Data Con Currency and LEC
54/77
54 2011 IBM Corporation
Cursor stability with currently committed (CC) semantics
Cursor stability withoutcurrently committed
seat name ...
7C Susan7B _____
...
reservations
App A App B
X
update
reservationsset name = 'John'where seat = '7C'
select namefrom reservations
where seat = '7C'S
Cursor stability withcurrently committed (Default behavior)
seat name ...7C Susan
7B _____
...
reservations
App AApp B
John
LockWait
App B hangs until App A commits or rolls back which releases X lock
Xupdatereservations
set name = 'John'where seat = '7C'
John
8/4/2019 1.5 - Data Con Currency and LEC
55/77
55 2011 IBM Corporation
Cursor stability with currently committed (CC) semantics
Cursor stability withoutcurrently committed
seat name ...
7C Susan7B _____
...
reservations
App A App B
X
update
reservationsset name = 'John'where seat = '7C'
select namefrom reservations
where seat = '7C'S
Cursor stability withcurrently committed (Default behavior)
seat name ...7C Susan
7B _____
...
reservations
App AApp B
John
LockWait
App B hangs until App A commits or rolls back which releases X lock
Xupdatereservations
set name = 'John'where seat = '7C'
John select namefrom reservationswhere seat = '7C'
S
8/4/2019 1.5 - Data Con Currency and LEC
56/77
56 2011 IBM Corporation
Cursor stability with currently committed (CC) semantics
Cursor stability withoutcurrently committed
seat name ...
7C Susan7B _____
...
reservations
App A App B
X
update
reservationsset name = 'John'where seat = '7C'
select namefrom reservations
where seat = '7C'S
Cursor stability withcurrently committed (Default behavior)
seat name ...7C Susan
7B _____
...
reservations
App AApp B
John
LockWait
App B hangs until App A commits or rolls back which releases X lock
Xupdatereservations
set name = 'John'where seat = '7C'
John select namefrom reservationswhere seat = '7C'
S
App B retrieves currently committed value of 'Susan'
8/4/2019 1.5 - Data Con Currency and LEC
57/77
57 2011 IBM Corporation
Comparing and choosing an isolation level
Isolation Level Lostupdate
DirtyRead
Non-repeatableRead
Phantom Read
Repeatable Read (RR)
- - - -ReadStability (RS) - - - Possible
Cursor Stability (CS) - - Possible Possible
Uncommitted Read (UR) - Possible Possible Possible
Application Type High data stability
required
High data stability not
required
Read-write transactions RS CS
Read-only transactions RS or RR UR
8/4/2019 1.5 - Data Con Currency and LEC
58/77
58 2011 IBM Corporation
Transactions
Concurrency & Locking Lock Wait
Deadlocks
Agenda
8/4/2019 1.5 - Data Con Currency and LEC
59/77
59 2011 IBM Corporation
Lock wait
By default, an application waits indefinitely to obtain anyneeded locks
LOCKTIMEOUT (db cfg):
Specifies the number of seconds to wait for a lock
Default value is -1 or infinitewait
Example: (Same as when using isolation CS without CC):
seat name ...
7C Susan7B _____
...
reservations
App A App B
8/4/2019 1.5 - Data Con Currency and LEC
60/77
60 2011 IBM Corporation
Lock wait
By default, an application waits indefinitely to obtain anyneeded locks
LOCKTIMEOUT (db cfg):
Specifies the number of seconds to wait for a lock
Default value is -1 or infinitewait
Example: (Same as when using isolation CS without CC):
seat name ...
7C Susan7B _____
...
reservations
App A App B
X
update
reservationsset name = 'John'
where seat = '7C'
8/4/2019 1.5 - Data Con Currency and LEC
61/77
61 2011 IBM Corporation
Lock wait
By default, an application waits indefinitely to obtain anyneeded locks
LOCKTIMEOUT (db cfg):
Specifies the number of seconds to wait for a lock
Default value is -1 or infinitewait
Example: (Same as when using isolation CS without CC):
seat name ...
7C Susan7B _____
...
reservations
App A App B
X
update
reservationsset name = 'John'
where seat = '7C'
John
8/4/2019 1.5 - Data Con Currency and LEC
62/77
62 2011 IBM Corporation
Lock wait
By default, an application waits indefinitely to obtain anyneeded locks
LOCKTIMEOUT (db cfg):
Specifies the number of seconds to wait for a lock
Default value is -1 or infinitewait
Example: (Same as when using isolation CS without CC):
seat name ...
7C Susan7B _____
...
reservations
App A App B
X
update
reservationsset name = 'John'
where seat = '7C'
Johnselect namefrom reservationswhere seat = '7C'
S
8/4/2019 1.5 - Data Con Currency and LEC
63/77
63 2011 IBM Corporation
Lock wait
By default, an application waits indefinitely to obtain anyneeded locks
LOCKTIMEOUT (db cfg):
Specifies the number of seconds to wait for a lock
Default value is -1 or infinitewait
Example: (Same as when using isolation CS without CC):
seat name ...
7C Susan7B _____
...
reservations
App A App B
X
update
reservationsset name = 'John'
where seat = '7C'
Johnselect namefrom reservationswhere seat = '7C'
S
LockWait
8/4/2019 1.5 - Data Con Currency and LEC
64/77
64 2011 IBM Corporation
Lock wait
By default, an application waits indefinitely to obtain anyneeded locks
LOCKTIMEOUT (db cfg):
Specifies the number of seconds to wait for a lock
Default value is -1 or infinitewait
Example: (Same as when using isolation CS without CC):
seat name ...
7C Susan7B _____
...
reservations
App A App B
X
update
reservationsset name = 'John'
where seat = '7C'
select namefrom reservationswhere seat = '7C'
SJohn
App B waits LOCKTIMEOUT seconds to get 'S' lock on first row
LockWait
8/4/2019 1.5 - Data Con Currency and LEC
65/77
65 2011 IBM Corporation
Transactions
Concurrency & Locking Lock Wait
Deadlocks
Agenda
8/4/2019 1.5 - Data Con Currency and LEC
66/77
66 2011 IBM Corporation
Deadlocks Occurs when two or more applications wait indefinitely for a resource
Each application is holding a resource that the other needs
Waiting is never resolved
In the example, assume we are using isolation CS without CC
seat name ...
7C Susan
7B _____
...
8E Raul
9F Jin
App A App Breservations
8/4/2019 1.5 - Data Con Currency and LEC
67/77
67 2011 IBM Corporation
Deadlocks
seat name ...
7C Susan
7B _____
...
8E Raul
9F Jin
App A App B
Xupdatereservationsset name = 'John'where seat = '7C'
reservations
Occurs when two or more applications wait indefinitely for a resource
Each application is holding a resource that the other needs
Waiting is never resolved
In the example, assume we are using isolation CS without CC
8/4/2019 1.5 - Data Con Currency and LEC
68/77
68 2011 IBM Corporation
Deadlocks
seat name ...
7C Susan
7B _____
...
8E Raul
9F Jin
App A App B
Xupdatereservationsset name = 'John'where seat = '7C'
reservations
John
Occurs when two or more applications wait indefinitely for a resource
Each application is holding a resource that the other needs
Waiting is never resolved
In the example, assume we are using isolation CS without CC
8/4/2019 1.5 - Data Con Currency and LEC
69/77
69 2011 IBM Corporation
Deadlocks
seat name ...
7C Susan
7B _____
...
8E Raul
9F Jin
App A App B
Xupdatereservationsset name = 'John'where seat = '7C'
reservations
John updatereservationsset name = 'Sue'where seat = '9F'
X
Occurs when two or more applications wait indefinitely for a resource
Each application is holding a resource that the other needs
Waiting is never resolved
In the example, assume we are using isolation CS without CC
8/4/2019 1.5 - Data Con Currency and LEC
70/77
70 2011 IBM Corporation
Deadlocks
seat name ...
7C Susan
7B _____
...
8E Raul
9F Jin
App A App B
Xupdatereservationsset name = 'John'where seat = '7C'
reservations
John updatereservationsset name = 'Sue'where seat = '9F'
XSue
Occurs when two or more applications wait indefinitely for a resource
Each application is holding a resource that the other needs
Waiting is never resolved
In the example, assume we are using isolation CS without CC
8/4/2019 1.5 - Data Con Currency and LEC
71/77
71 2011 IBM Corporation
Deadlocks
seat name ...
7C Susan
7B _____
...
8E Raul
9F Jin
App A App B
Xupdatereservationsset name = 'John'where seat = '7C'
reservations
John updatereservationsset name = 'Sue'where seat = '9F'
XSueS
...select namefrom reservationswhere seat = '9F'
Occurs when two or more applications wait indefinitely for a resource
Each application is holding a resource that the other needs
Waiting is never resolved
In the example, assume we are using isolation CS without CC
8/4/2019 1.5 - Data Con Currency and LEC
72/77
72 2011 IBM Corporation
Deadlocks
seat name ...
7C Susan
7B _____
...
8E Raul
9F Jin
App A App B
Xupdatereservationsset name = 'John'where seat = '7C'
reservations
John updatereservationsset name = 'Sue'where seat = '9F'
XSueS
...select namefrom reservationswhere seat = '9F'
LockWait
Occurs when two or more applications wait indefinitely for a resource
Each application is holding a resource that the other needs
Waiting is never resolved
In the example, assume we are using isolation CS without CC
8/4/2019 1.5 - Data Con Currency and LEC
73/77
73 2011 IBM Corporation
Deadlocks
seat name ...
7C Susan
7B _____
...
8E Raul
9F Jin
App A App B
Xupdatereservationsset name = 'John'where seat = '7C'
reservations
John updatereservationsset name = 'Sue'where seat = '9F'
XSueS
...select namefrom reservationswhere seat = '9F'
LockWait
...select namefrom reservationswhere seat = '7C'
S
Occurs when two or more applications wait indefinitely for a resource
Each application is holding a resource that the other needs
Waiting is never resolved
In the example, assume we are using isolation CS without CC
8/4/2019 1.5 - Data Con Currency and LEC
74/77
74 2011 IBM Corporation
Deadlocks
seat name ...
7C Susan
7B _____
...
8E Raul
9F Jin
App A App B
Xupdatereservationsset name = 'John'where seat = '7C'
reservations
John updatereservationsset name = 'Sue'where seat = '9F'
XSueS
...select namefrom reservationswhere seat = '9F'
LockWait
...select namefrom reservationswhere seat = '7C'
S
Lock
Wait
Occurs when two or more applications wait indefinitely for a resource
Each application is holding a resource that the other needs
Waiting is never resolved
In the example, assume we are using isolation CS without CC
8/4/2019 1.5 - Data Con Currency and LEC
75/77
75 2011 IBM Corporation
Deadlocks
seat name ...
7C Susan
7B _____
...
8E Raul
9F Jin
App A App B
Xupdatereservationsset name = 'John'where seat = '7C'
...select namefrom reservationswhere seat = '7C'
SJohn
S
...select namefrom reservationswhere seat = '9F'
updatereservations
set name = 'Sue'where seat = '9F'
XSue
reservations
LockWait
Lock
Wait
Deadlock!
Occurs when two or more applications wait indefinitely for a resource
Each application is holding a resource that the other needs
Waiting is never resolved
In the example, assume we are using isolation CS without CC
8/4/2019 1.5 - Data Con Currency and LEC
76/77
76 2011 IBM Corporation
Deadlocks
Deadlocks are commonly caused by poor applicationdesign
DB2 provides a deadlock detectorUse DLCHKTIME (db cfg) to set the time interval for checking for
deadlocks
When a deadlock is detected, DB2 uses an internal algorithm to pickwhich transaction to roll back, and which one to continue.
The transaction that is forced to roll back gets a SQL error. The
rollback causes all of its locks to be released
8/4/2019 1.5 - Data Con Currency and LEC
77/77
Thank you!