Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 77

Data concurrency and locking

IBM Information Management Cloud Computing Center of Competence


IBM Canada Labs
1 © 2011 IBM Corporation
Agenda

• Transactions
• Concurrency & Locking
• Lock Wait
• Deadlocks

2 © 2011 IBM Corporation


Supporting reading material & videos
• 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

3 © 2011 IBM Corporation


Agenda

• Transactions
• Concurrency & Locking
• Lock Wait
• Deadlocks

4 © 2011 IBM Corporation


What is a transaction?

Your bank account Your Mom’s bank account

Balance = $1000 Balance = $200

Transfer $100 from your account to your Mom’s account:


- Debit $100 from your bank account (Subtract $100)
- Credit $100 to your mom's bank account (Add $100)

5 © 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

6 © 2011 IBM Corporation


Example of transactions
First SQL st atemen
starts trans t
action
INSERT INTO employee VALUES (100, 'JOHN' )
INSERT INTO employee VALUES (200, 'MANDY')
COMMIT
No changes
applied due to
ROLLBA K
C
DELETE FROM employee WHERE name='MAND Y'
UPDATE employee SET empID=101 where name='JOHN'
ROLLBACK

UPDATE employee SET name='JACK' where empID=100


COMMIT There is not hing to
rollback
ROLLBACK
7 © 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 © 2011 IBM Corporation
Agenda

• Transactions
• Concurrency & Locking
• Lock Wait
• Deadlocks

9 © 2011 IBM Corporation


Concurrency and Locking

App A
ID Name Age

App B 3 Peter 33
5 John 23
App C 22 Mary 22
35 Ann 55
App

• Concurr
ency:
• Multiple users accessing the same resources at the
same time
10
• Locking: © 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

11 © 2011 IBM Corporation


Problems if there is no concurrency control

• Lost update
• Uncommitted read
• Non-repeatable read
• Phantom read

12 © 2011 IBM Corporation


Lost update
reservations
seat name ...
7C
7B
...

App A App B

13 © 2011 IBM Corporation


Lost update
reservations
seat name ...
7C
7B
...

App A App B

update reservations
set name =
'John' where
seat = '7C'

14 © 2011 IBM Corporation


Lost update
reservations
seat name ...
7C _____
John
7B
...

App A App B

update reservations
set name =
'John' where
seat = '7C'

15 © 2011 IBM Corporation


Lost update
reservations
seat name ...
7C _____
John
7B
...

App A App B

update reservations update reservations


set name = set name =
'John' where 'Mary' where
seat = '7C' seat = '7C'

16 © 2011 IBM Corporation


Lost update
reservations
seat name ...
7C _____
Mary
Joh
7B n
...

App A App B

update reservations update reservations


set name = set name =
'John' where 'Mary' where
seat = '7C' seat = '7C'

17 © 2011 IBM Corporation


Lost update
reservations
seat name ...
7C M? ary
_____
Joh
7B n
...

App A App B

update reservations update reservations


set name = set name =
'John' where 'Mary' where
seat = '7C' seat = '7C'

18 © 2011 IBM Corporation


Uncommitted read (also known as “dirty read”)
reservations
seat name ...
7C
7B
...

App A App B

19 © 2011 IBM Corporation


Uncommitted read (also known as “dirty read”)
reservations
seat name ...
7C
7B
...

App A App B
update reservations
set name =
'John' where
seat = '7C'

20 © 2011 IBM Corporation


Uncommitted read (also known as “dirty read”)
reservations
seat name ...
7C _____
John
7B
...

App A App B
update reservations
set name =
'John' where
seat = '7C'

21 © 2011 IBM Corporation


Uncommitted read (also known as “dirty read”)
reservations
seat name ...
7C _____
John
7B
...

App A App B
update reservations Select name
set name = from reservations
'John' where where seat is
seat = '7C' '7C'

22 © 2011 IBM Corporation


Uncommitted read (also known as “dirty read”)
reservations
seat name ...
7C _____
John
7B
...

App A App B
update reservations Select name
set name = from reservations John
'John' where where seat is
seat = '7C' '7C'

23 © 2011 IBM Corporation


Uncommitted read (also known as “dirty read”)
reservations
seat name ...
7C _____
John
7B
...

App A App B
update reservations Select name
set name = from reservations John
'John' where where seat is
seat = '7C' '7C'

Roll back

24 © 2011 IBM Corporation


Uncommitted read (also known as “dirty read”)
reservations
seat name ...
7C
7B
...

App A App B
update reservations Select name
set name = from reservations John
'John' where where seat is
seat = '7C' '7C'

Roll back

25 © 2011 IBM Corporation


Uncommitted read (also known as “dirty read”)
reservations
seat name ...
7C
7B
...

App A App B
update reservations Select name
set name = from reservations John
'John' where where seat is
seat = '7C' '7C' processing in Ap p
Further
B uses incorrect /
uncommitted value of
Roll back “John”

26 © 2011 IBM Corporation


Non-repeatable read
reservations
seat name ...
7C
7B
...

App A App B

27 © 2011 IBM Corporation


Non-repeatable read
reservations
seat name ...
7C
7B
...

App A App B

select seat
from reservations
where name is NULL

28 © 2011 IBM Corporation


Non-repeatable read
reservations
seat name ...
7C
7B
...

App A App B

7C select seat
from reservations
7B
where name is NULL

29 © 2011 IBM Corporation


Non-repeatable read
reservations
seat name ...
7C
7B
...

App A App B

7C select seat update reservations


from reservations set name =
7B 'John' where
where name is NULL
seat = '7C'

30 © 2011 IBM Corporation


Non-repeatable read
reservations
seat name ...
7C _____
John
7B
...

App A App B

7C select seat update reservations


from reservations set name =
7B 'John' where
where name is NULL
seat = '7C'

31 © 2011 IBM Corporation


Non-repeatable read
reservations
seat name ...
7C _____
John
7B
...

App A App B

7C select seat update reservations


from reservations set name =
7B 'John' where
where name is NULL
seat = '7C'
...
select seat
from reservations
where name is NULL

32 © 2011 IBM Corporation


Non-repeatable read
reservations
seat name ...
7C _____
John
7B
...

App A App B

7C select seat update reservations


from reservations set name =
7B 'John' where
where name is NULL
seat = '7C'
...
select seat
7B from reservations
where name is NULL

33 © 2011 IBM Corporation


Non-repeatable read
reservations
seat name ...
7C _____
John
7B
...

App A App B

7C select seat update reservations


from reservations set name =
7B 'John' where
where name is NULL
seat = '7C'
... The same SELECT (read) returns a
select seat different result: Less rows (in this
7B from reservations case '7C' doesn't show anymore).
where name is NULL This is a non-repeatable read

34 © 2011 IBM Corporation


Phantom read
reservations
seat name ...
7C _____
Susan
7B
...

App A App B

35 © 2011 IBM Corporation


Phantom read
reservations
seat name ...
7C _____
Susan
7B
...

App A App B

select seat
from reservations
where name is NULL

36 © 2011 IBM Corporation


Phantom read
reservations
seat name ...
7C _____
Susan
7B
...

App A App B

select seat
7B from reservations
where name is NULL

37 © 2011 IBM Corporation


Phantom read
reservations
seat name ...
7C _____
Susan
7B
...

App A App B

select seat update reservations


7B from reservations set name = NULL
where name is NULL where seat = '7C'

38 © 2011 IBM Corporation


Phantom read
reservations
seat name ...
7C
7B
...

App A App B

select seat update reservations


7B from reservations set name = NULL
where name is NULL where seat = '7C'

39 © 2011 IBM Corporation


Phantom read
reservations
seat name ...
7C
7B
...

App A App B

select seat update reservations


7B from reservations set name = NULL
where name is NULL where seat = '7C'
...
select seat
from reservations
where name is NULL

40 © 2011 IBM Corporation


Phantom read
reservations
seat name ...
7C
7B
...

App A App B

select seat update reservations


7B from reservations set name = NULL
where name is NULL where seat = '7C'
...
select seat
7C
from reservations
7B where name is NULL

41 © 2011 IBM Corporation


Phantom read
reservations
seat name ...
7C
7B
...

App A App B

select seat update reservations


7B from reservations set name = NULL
where name is NULL where seat = '7C'

... The same SELECT (read) returns a


select seat different result: More rows (phantom
7C rows, in this case '7C', is shown)
from
7B reservations
where name is NULL This is a phantom read

42 © 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)

43 © 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:

SELECT COUNT(*) FROM tab1 WITH UR

• For embedded SQL, the level is set at bind time


• For dynamic SQL, the level is set at run time

44 © 2011 IBM Corporation


Comparing isolation levels

45 © 2011 IBM Corporation


Cursor stability with currently committed (CC) semantics
• Cursor stability with currently committed semantics is the default
isolation level
• Use cur_commit db cfg parameter to enable/disable
• Avoids timeouts and deadlocks

Cursor Cursor stability with


stability currently
committed

46 © 2011 IBM Corporation


Cursor stability with currently committed (CC) semantics
Cursor stability without currently committed
reservations
App A seat name ...
App B 7C Susan
7B
...

Cursor stability with currently committed (Default behavior)


reservations
App A seat name ...
App B 7C Susan
7B
...

47 © 2011 IBM Corporation


Cursor stability with currently committed (CC) semantics
Cursor stability without currently committed
reservations
App A seat name ...
update
App B 7C Susan
reservations X
set name = 'John' 7B
where seat = ...
'7C'

Cursor stability with currently committed (Default behavior)


reservations
App A seat name ...
App B 7C Susan
7B
...

48 © 2011 IBM Corporation


Cursor stability with currently committed (CC) semantics
Cursor stability without currently committed
reservations
App A seat name ...
update
App B 7C Susan
John
reservations X
set name = 'John' 7B
where seat = ...
'7C'

Cursor stability with currently committed (Default behavior)


reservations
App A seat name ...
App B 7C Susan
7B
...

49 © 2011 IBM Corporation


Cursor stability with currently committed (CC) semantics
Cursor stability without currently committed
reservations
App A seat name ...
update
App B 7C select name
Susan
John S
reservations X from reservations
set name = 'John' 7B
where seat =
where seat = '7C'
'7C' ...

Cursor stability with currently committed (Default behavior)


reservations
App A seat name ...
App B 7C Susan
7B
...

50 © 2011 IBM Corporation


Cursor stability with currently committed (CC) semantics
Cursor stability without currently committed
reservations
Lock
App A seat name ... App B
update 7C Wait select name
Susan
John S
reservations X from reservations
set name = 'John' 7B
where seat =
where seat = '7C'
'7C' ...

Cursor stability with currently committed (Default behavior)


reservations
App A seat name ...
App B 7C Susan
7B
...

51 © 2011 IBM Corporation


Cursor stability with currently committed (CC) semantics
Cursor stability without currently committed
reservations
Lock
App A seat name ... App B
update Wait select name
7C John
Susan S
reservations X from reservations
set name = 'John' 7B
where seat =
where seat = ... '7C'
'7C'
App B hangs until App A commits or rolls back which releases X lock

Cursor stability with currently committed (Default behavior)


reservations
App A seat name ...
App B 7C Susan
7B
...

52 © 2011 IBM Corporation


Cursor stability with currently committed (CC) semantics
Cursor stability without currently committed
reservations
Lock
App A seat name ... App B
update Wait select name
7C John
Susan S
reservations X from reservations
set name = 'John' 7B
where seat =
where seat = ... '7C'
'7C'
App B hangs until App A commits or rolls back which releases X lock

Cursor stability with currently committed (Default behavior)


reservations
App A seat name ...
update
App B 7C Susan
reservations X
set name = 'John' 7B
where seat = ...
'7C'
53 © 2011 IBM Corporation
Cursor stability with currently committed (CC) semantics
Cursor stability without currently committed
reservations
Lock
App A seat name ... App B
update Wait select name
7C John
Susan S
reservations X from reservations
set name = 'John' 7B
where seat =
where seat = ... '7C'
'7C'
App B hangs until App A commits or rolls back which releases X lock

Cursor stability with currently committed (Default behavior)


reservations
App A seat name ...
update
App B 7C
X
John
Susa
reservations n
set name = 'John' 7B
where seat = ...
'7C'
54 © 2011 IBM Corporation
Cursor stability with currently committed (CC) semantics
Cursor stability without currently committed
reservations
Lock
App A seat name ... App B
update Wait select name
7C John
Susan S
reservations X from reservations
set name = 'John' 7B
where seat =
where seat = ... '7C'
'7C'
App B hangs until App A commits or rolls back which releases X lock

Cursor stability with currently committed (Default behavior)


reservations
App A seat name ...
update
App B 7C select name
X
John
Susa S
reservations n from reservations
set name = 'John' 7B
where seat =
where seat = '7C'
'7C' ...
55 © 2011 IBM Corporation
Cursor stability with currently committed (CC) semantics
Cursor stability without currently committed
reservations
Lock
App A seat name ... App B
update Wait select name
7C John
Susan S
reservations X from reservations
set name = 'John' 7B
where seat =
where seat = ... '7C'
'7C'
App B hangs until App A commits or rolls back which releases X lock

Cursor stability with currently committed (Default behavior)


reservations
App A seat name ... App B
update select name
7C John
Susa S
reservations X from reservations
7B n
set name = 'John' where seat =
where seat = ... '7C'
'7C' App B retrieves currently committed value of 'Susan'
56 © 2011 IBM Corporation
Comparing and choosing an isolation level

Isolation Level Lost Dirty Non-repeatable Phantom Read


update Read
Read
Repeatable Read (RR) - - - -
ReadStability (RS) - - - Possible
Cursor Stability (CS) - - Possible Possible
Uncommitted Read (UR) - Possible Possible Possible

Application Type High data stability High data stability not


required required
Read-write transactions RS CS
Read-only transactions RS or RR UR

57 © 2011 IBM Corporation


Agenda

• Transactions
• Concurrency & Locking
• Lock Wait
• Deadlocks

58 © 2011 IBM Corporation


Lock wait
■ By default, an application waits indefinitely to obtain any
needed locks
■ LOCKTIMEOUT (db cfg):
– Specifies the number of seconds to wait for a lock
– Default value is -1 or infinite wait
■ Example: (Same as when using isolation CS without CC):
reservations
App A seat name ... App B
7C Susan
7B
...

59 © 2011 IBM Corporation


Lock wait
■ By default, an application waits indefinitely to obtain any
needed locks
■ LOCKTIMEOUT (db cfg):
– Specifies the number of seconds to wait for a lock
– Default value is -1 or infinite wait
■ Example: (Same as when using isolation CS without CC):
reservations
App A seat name ... App B
update 7C Susan
reservations X
set name = 'John' 7B
where seat = ...
'7C'

60 © 2011 IBM Corporation


Lock wait
■ By default, an application waits indefinitely to obtain any
needed locks
■ LOCKTIMEOUT (db cfg):
– Specifies the number of seconds to wait for a lock
– Default value is -1 or infinite wait
■ Example: (Same as when using isolation CS without CC):
reservations
App A seat name ... App B
update 7C Susa
John
reservations X
7B n
set name = 'John'
where seat = ...
'7C'

61 © 2011 IBM Corporation


Lock wait
■ By default, an application waits indefinitely to obtain any
needed locks
■ LOCKTIMEOUT (db cfg):
– Specifies the number of seconds to wait for a lock
– Default value is -1 or infinite wait
■ Example: (Same as when using isolation CS without CC):
reservations
App A seat name ... App B
update 7C select name
Susa
John S
reservations X from reservations
7B n
set name = 'John' where seat =
where seat = '7C'
'7C' ...

62 © 2011 IBM Corporation


Lock wait
■ By default, an application waits indefinitely to obtain any
needed locks
■ LOCKTIMEOUT (db cfg):
– Specifies the number of seconds to wait for a lock
– Default value is -1 or infinite wait
■ Example: (Same as when using isolation CS without CC):
reservations
Lock
App A seat name ... App B
update 7C
Wait
select name
Susa
John S
reservations X from reservations
7B n
set name = 'John' where seat =
where seat = '7C'
'7C' ...

63 © 2011 IBM Corporation


Lock wait
■ By default, an application waits indefinitely to obtain any
needed locks
■ LOCKTIMEOUT (db cfg):
– Specifies the number of seconds to wait for a lock
– Default value is -1 or infinite wait
■ Example: (Same as when using isolation CS without CC):
reservations
Lock
App A seat name ... App B
update 7C John
Wait
select name
Susa S
reservations X from reservations
7B n
set name = 'John' where seat =
where seat = '7C'
'7C' ...
App B waits “LOCKTIMEOUT” seconds to get 'S' lock on first row
64 © 2011 IBM Corporation
Agenda

• Transactions
• Concurrency & Locking
• Lock Wait
• Deadlocks

65 © 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
reservations
App A seat name ...
App B 7C Susan
7B
...
8E Raul
9F Jin

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
reservations
App A seat name ...
update
App B 7C Susan
reservations X
set name = 'John' 7B
where seat = ...
'7C' 8E Raul
9F Jin

67 © 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
reservations
App A seat name ...
update
App B 7C Susan
John
reservations X
set name = 'John' 7B
where seat = ...
'7C' 8E Raul
9F Jin

68 © 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
reservations
App A seat name ...
update
App B update
7C Susan
John
reservations X reservations
set name = 'John' 7B set name = 'Sue'
where seat = where seat =
'7C' ... '9F'
8E Raul
X
9F Jin

69 © 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
reservations
App A seat name ...
update
App B update
7C Susan
John
reservations X reservations
set name = 'John' 7B set name = 'Sue'
where seat = where seat =
'7C' ... '9F'
8E Raul
Jin X
9F Sue

70 © 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
reservations
App A seat name ...
update
App B update
7C Susan
John
reservations X reservations
set name = 'John' 7B set name = 'Sue'
where seat = where seat =
'7C' ... '9F'
... 8E Raul
S Jin X
select name 9F Sue
from reservations
where seat =
'9F' 71 © 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
reservations
App A seat name ...
update
App B update
7C Susan
John
reservations X reservations
set name = 'John' 7B set name = 'Sue'
where seat = where seat =
'7C' ... '9F'
... 8E Raul
S Jin X
select name Lock 9F Sue
from reservations
Wait
where seat =
'9F' 72 © 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
reservations
App A seat name ...
update
App B update
7C Susan
John S
reservations X reservations
set name = 'John' 7B set name = 'Sue'
where seat = where seat =
'7C' ... '9F'
... 8E Raul ...
S Jin X
select name Lock 9F Sue select name
from reservations from reservations
Wait
where seat = where seat =
'9F' 73 '7C' © 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
reservations
seat name ...
App A Lock App B
update
update 7C Susan
John Wait
reservations X reservations
7B S
set name = 'John' set name = 'Sue'
where seat = where seat =
'7C' ... '9F'
... 8E Raul ...
S Jin X
select name Lock 9F Sue select name
from reservations from reservations
Wait
where seat = where seat =
'9F' 74 '7C' © 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
reservations
App A seat name ... Lock App B
update update
7C Susan
John Wait
reservations X reservations
7B S
set name = 'John' set name = 'Sue'
where seat = ... where seat =
'7C' '9F'
8E Raul
... 9F Sue
Jin X ...
select name
S select name
Lock
from reservations from reservations
Wait
where seat = Deadlo ck! where seat =
'9F' 75 '7C' © 2011 IBM Corporation
Deadlocks

■ Deadlocks are commonly caused by poor application


design
■ DB2 provides a deadlock detector
– Use DLCHKTIME (db cfg) to set the time interval for checking for
deadlocks

– When a deadlock is detected, DB2 uses an internal algorithm to pick


which 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

76 © 2011 IBM Corporation


Thank you!
77 © 2011 IBM Corporation

You might also like