1.5 - Data Con Currency and LEC

You might also like

Download as pdf or txt
Download as 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

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

2011 IBM Corporation

Agenda
Transactions Concurrency & Locking Lock Wait Deadlocks

2011 IBM Corporation

What is a transaction?
Your bank account
Balance = $1000

Your Moms bank account


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)

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

2011 IBM Corporation

Example of transactions
First SQL statement starts transaction INSERT INTO employee VALUES (100, 'JOHN') INSERT INTO employee VALUES (200, 'MANDY') COMMIT No changes applied due to ROLLBACK DELETE FROM employee WHERE name='MANDY' UPDATE employee SET empID=101 where name='JOHN' ROLLBACK

UPDATE employee SET name='JACK' where empID=100 COMMIT There is nothing 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

2011 IBM Corporation

Concurrency and Locking


App A
ID Name Peter John Mary Ann Age 33 23 22 55

App B App C App D

3 5 22 35

Concurrency: Multiple users accessing the same resources at the same time Locking: Mechanism to ensure data integrity and consistency
10
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 7C 7B ...

name _____ _____

...

App A

App B

13

2011 IBM Corporation

Lost update
reservations

seat 7C 7B ...

name _____ _____

...

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

App B

14

2011 IBM Corporation

Lost update
reservations

seat 7C 7B ...

name _____ John _____

...

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

App B

15

2011 IBM Corporation

Lost update
reservations

seat 7C 7B ...

name _____ John _____

...

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

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

16

2011 IBM Corporation

Lost update
reservations

seat 7C 7B ...

name _____ Mary John _____

...

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

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

17

2011 IBM Corporation

Lost update
reservations

seat 7C 7B ...

name _____ Mary John

...

? _____
App B update reservations set name = 'Mary' where seat = '7C'

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

18

2011 IBM Corporation

Uncommitted read (also known as dirty read)


reservations

seat 7C 7B ...
App A

name _____ _____

...

App B

19

2011 IBM Corporation

Uncommitted read (also known as dirty read)


reservations

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

name _____ _____

...

App B

20

2011 IBM Corporation

Uncommitted read (also known as dirty read)


reservations

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

name _____ John _____

...

App B

21

2011 IBM Corporation

Uncommitted read (also known as dirty read)


reservations

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

name _____ John _____

...

App B Select name from reservations where seat is '7C'

22

2011 IBM Corporation

Uncommitted read (also known as dirty read)


reservations

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

name _____ John _____

...

App B Select name from reservations where seat is '7C'

John

23

2011 IBM Corporation

Uncommitted read (also known as dirty read)


reservations

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

name _____ John _____

...

App B Select name from reservations where seat is '7C'

John

Roll back

24

2011 IBM Corporation

Uncommitted read (also known as dirty read)


reservations

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

name _____ _____

...

App B Select name from reservations where seat is '7C'

John

Roll back

25

2011 IBM Corporation

Uncommitted read (also known as dirty read)


reservations

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

name _____ _____

...

App B Select name from reservations where seat is '7C'


Further processing in App Further processing in App B uses incorrect / / B uses incorrect uncommitted value of uncommitted value of John John
2011 IBM Corporation

John

Roll back

26

Non-repeatable read
reservations

seat 7C 7B ...
App A

name _____ _____

...

App B

27

2011 IBM Corporation

Non-repeatable read
reservations

seat 7C 7B ...
App A

name _____ _____

...

App B

select seat from reservations where name is NULL

28

2011 IBM Corporation

Non-repeatable read
reservations

seat 7C 7B ...
App A
7C 7B

name _____ _____

...

App B

select seat from reservations where name is NULL

29

2011 IBM Corporation

Non-repeatable read
reservations

seat 7C 7B ...
App A
7C 7B

name _____ _____

...

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

select seat from reservations where name is NULL

30

2011 IBM Corporation

Non-repeatable read
reservations

seat 7C 7B ...
App A
7C 7B

name _____ John _____

...

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

select seat from reservations where name is NULL

31

2011 IBM Corporation

Non-repeatable read
reservations

seat 7C 7B ...
App A
7C 7B

name _____ John _____

...

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

select seat from reservations where name is NULL ... select seat from reservations where name is NULL

32

2011 IBM Corporation

Non-repeatable read
reservations

seat 7C 7B ...
App A
7C 7B

name _____ John _____

...

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

select seat from reservations where name is NULL ... select seat from reservations where name is NULL

7B

33

2011 IBM Corporation

Non-repeatable read
reservations

seat 7C 7B ...
App A
7C 7B

name _____ John _____

...

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


The same SELECT (read) returns aa The same SELECT (read) returns different result: Less rows (in this different result: Less rows (in this case '7C' doesn't show anymore). case '7C' doesn't show anymore). This is aa non-repeatable read This is non-repeatable read
2011 IBM Corporation

select seat from reservations where name is NULL ... select seat from reservations where name is NULL

7B

34

Phantom read
reservations

seat 7C 7B ...
App A

name _____ Susan _____

...

App B

35

2011 IBM Corporation

Phantom read
reservations

seat 7C 7B ...
App A

name _____ Susan _____

...

App B

select seat from reservations where name is NULL

36

2011 IBM Corporation

Phantom read
reservations

seat 7C 7B ...
App A
7B

name _____ Susan _____

...

App B

select seat from reservations where name is NULL

37

2011 IBM Corporation

Phantom read
reservations

seat 7C 7B ...
App A
7B

name _____ Susan _____

...

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

select seat from reservations where name is NULL

38

2011 IBM Corporation

Phantom read
reservations

seat 7C 7B ...
App A
7B

name _____ _____

...

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

select seat from reservations where name is NULL

39

2011 IBM Corporation

Phantom read
reservations

seat 7C 7B ...
App A
7B

name _____ _____

...

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

select seat from reservations where name is NULL ... select seat from reservations where name is NULL

40

2011 IBM Corporation

Phantom read
reservations

seat 7C 7B ...
App A
7B

name _____ _____

...

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

select seat from reservations where name is NULL ... select seat from reservations where name is NULL

7C 7B
41

2011 IBM Corporation

Phantom read
reservations

seat 7C 7B ...
App A
7B

name _____ _____

...

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

select seat from reservations where name is NULL

7C 7B
42

The same SELECT (read) returns aa ... The same SELECT (read) returns different result: More rows (phantom select seat different result: More rows (phantom rows, in this case '7C', is shown) from reservations rows, in this case '7C', is shown) This is aa phantom read where name is NULL This is phantom read
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 stability Cursor stability with currently committed

46

2011 IBM Corporation

Cursor stability with currently committed (CC) semantics


Cursor stability without currently committed
reservations

App A

seat 7C 7B ...

name Susan _____

...

App B

Cursor stability with currently committed (Default behavior)


reservations

App A

seat 7C 7B ...

name Susan _____

...

App B

47

2011 IBM Corporation

Cursor stability with currently committed (CC) semantics


Cursor stability without currently committed
reservations

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

seat

name Susan _____

...

App B

7C 7B ...

Cursor stability with currently committed (Default behavior)


reservations

App A

seat 7C 7B ...

name Susan _____

...

App B

48

2011 IBM Corporation

Cursor stability with currently committed (CC) semantics


Cursor stability without currently committed
reservations

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

seat

name Susan John _____

...

App B

7C 7B ...

Cursor stability with currently committed (Default behavior)


reservations

App A

seat 7C 7B ...

name Susan _____

...

App B

49

2011 IBM Corporation

Cursor stability with currently committed (CC) semantics


Cursor stability without currently committed
reservations

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

seat

name Susan John _____

...

App B S
select name from reservations where seat = '7C'

7C 7B ...

Cursor stability with currently committed (Default behavior)


reservations

App A

seat 7C 7B ...

name Susan _____

...

App B

50

2011 IBM Corporation

Cursor stability with currently committed (CC) semantics


Cursor stability without currently committed
reservations

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

seat

name Susan John _____

...

Lock Wait

App B
select name from reservations where seat = '7C'

7C 7B ...

Cursor stability with currently committed (Default behavior)


reservations

App A

seat 7C 7B ...

name Susan _____

...

App B

51

2011 IBM Corporation

Cursor stability with currently committed (CC) semantics


Cursor stability without currently committed
reservations

App A

seat

name

...

update select name Susan X 7C John S reservations from reservations 7B _____ set name = 'John' where seat = '7C' where seat = '7C' ... App B hangs until App A commits or rolls back which releases X lock

Lock Wait

App B

Cursor stability with currently committed (Default behavior)


reservations

App A

seat 7C 7B ...

name Susan _____

...

App B

52

2011 IBM Corporation

Cursor stability with currently committed (CC) semantics


Cursor stability without currently committed
reservations

App A

seat

name

...

update select name Susan X 7C John S reservations from reservations 7B _____ set name = 'John' where seat = '7C' where seat = '7C' ... App B hangs until App A commits or rolls back which releases X lock

Lock Wait

App B

Cursor stability with currently committed (Default behavior)


reservations

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

seat

name Susan _____

...

App B

7C 7B ...

2011 IBM Corporation

Cursor stability with currently committed (CC) semantics


Cursor stability without currently committed
reservations

App A

seat

name

...

update select name Susan X 7C John S reservations from reservations 7B _____ set name = 'John' where seat = '7C' where seat = '7C' ... App B hangs until App A commits or rolls back which releases X lock

Lock Wait

App B

Cursor stability with currently committed (Default behavior)


reservations

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

seat

name Susan John _____

...

App B

7C 7B ...

2011 IBM Corporation

Cursor stability with currently committed (CC) semantics


Cursor stability without currently committed
reservations

App A

seat

name

...

update select name Susan X 7C John S reservations from reservations 7B _____ set name = 'John' where seat = '7C' where seat = '7C' ... App B hangs until App A commits or rolls back which releases X lock

Lock Wait

App B

Cursor stability with currently committed (Default behavior)


reservations

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

seat

name Susan John _____

...

App B S
select name from reservations where seat = '7C'

7C 7B ...

2011 IBM Corporation

Cursor stability with currently committed (CC) semantics


Cursor stability without currently committed
reservations

App A

seat

name

...

update select name Susan X 7C John S reservations from reservations 7B _____ set name = 'John' where seat = '7C' where seat = '7C' ... App B hangs until App A commits or rolls back which releases X lock

Lock Wait

App B

Cursor stability with currently committed (Default behavior)


reservations

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

seat

name Susan John _____

...

App B S
select name from reservations where seat = '7C'

7C 7B ...

App B retrieves currently committed value of 'Susan'


2011 IBM Corporation

Comparing and choosing an isolation level


Isolation Level Repeatable Read (RR) ReadStability (RS) Cursor Stability (CS) Uncommitted Read (UR) Lost Dirty update Read Non-repeatable Phantom Read Read

Possible

Possible Possible

Possible Possible Possible

Application Type Read-write transactions Read-only transactions

High data stability required RS RS or RR

High data stability not required CS 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 7C 7B ...

name Susan _____

...

App B

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
update reservations set name = 'John' where seat = '7C'

seat

name Susan _____

...

App B

7C 7B ...

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
update reservations set name = 'John' where seat = '7C'

seat

name Susan John _____

...

App B

7C 7B ...

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
update reservations set name = 'John' where seat = '7C'

seat

name Susan John _____

...

App B S
select name from reservations where seat = '7C'

7C 7B ...

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

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

seat

name Susan John _____

...

Lock Wait

App B
select name from reservations where seat = '7C'

7C 7B ...

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

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

seat

name Susan John _____

...

Lock Wait

App B
select name from reservations where seat = '7C'

7C 7B ...

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 7C 7B ... 8E 9F

name Susan _____ Raul Jin

...

App B

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
update reservations set name = 'John' where seat = '7C'

seat

name Susan _____ Raul Jin

...

App B

7C 7B ... 8E 9F

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
update reservations set name = 'John' where seat = '7C'

seat

name Susan John _____ Raul Jin

...

App B

7C 7B ... 8E 9F

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
update reservations set name = 'John' where seat = '7C'

seat

name Susan John _____ Raul Jin

...

App B
update reservations set name = 'Sue' where seat = '9F'

7C 7B ... 8E 9F

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
update reservations set name = 'John' where seat = '7C'

seat

name Susan John _____ Raul Jin Sue

...

App B
update reservations set name = 'Sue' where seat = '9F'

7C 7B ... 8E 9F

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
update reservations set name = 'John' where seat = '7C' ... select name from reservations where seat = '9F'
71

seat

name Susan John _____ Raul Jin Sue

...

App B
update reservations set name = 'Sue' where seat = '9F'

7C 7B ... 8E

9F

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
update reservations set name = 'John' where seat = '7C' ... select name from reservations where seat = '9F'
72

seat

name Susan John _____ Raul Jin Sue

...

App B
update reservations set name = 'Sue' where seat = '9F'

7C 7B ... 8E

Lock Wait

9F

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
update reservations set name = 'John' where seat = '7C' ... select name from reservations where seat = '9F'
73

seat

name Susan John _____ Raul Jin Sue

...

App B S
update reservations set name = 'Sue' where seat = '9F' ... select name from reservations where seat = '7C'
2011 IBM Corporation

7C 7B ... 8E

Lock Wait

9F

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
update reservations set name = 'John' where seat = '7C' ... select name from reservations where seat = '9F'
74

seat

name Susan John _____ Raul Jin Sue

...

7C 7B ... 8E

Lock Wait

App B
update reservations set name = 'Sue' where seat = '9F' ... select name from reservations where seat = '7C'
2011 IBM Corporation

Lock Wait

9F

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
update reservations set name = 'John' where seat = '7C' ... select name from reservations where seat = '9F'
75

seat

name Susan John _____ Raul Jin Sue

...

7C 7B ... 8E

Lock Wait

App B
update reservations set name = 'Sue' where seat = '9F' ... select name from reservations where seat = '7C'
2011 IBM Corporation

Lock Wait

9F

Deadlock!

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