• Tidak ada hasil yang ditemukan

Data concurrency and locking

N/A
N/A
Protected

Academic year: 2018

Membagikan "Data concurrency and locking"

Copied!
77
0
0

Teks penuh

(1)

1 © 2011 IBM Corporation

Data concurrency and locking

(2)

• Transactions

• Concurrency & Locking

• Lock Wait

• Deadlocks

(3)

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

(4)

• Transactions

Concurrency & Locking

Lock Wait

Deadlocks

(5)

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

(6)

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

(7)

7 © 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 statement starts transaction

No changes applied due to

ROLLBACK

(8)

Transactions – ACID rules

A

tomicity

• 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.

C

onsistency

• Any transaction will take the data from one consistent state to another, so only valid consistent data is stored in the database

I

solation

• Concurrent transactions cannot interfere with each other

(9)

9 © 2011 IBM Corporation

Transactions

• Concurrency & Locking

Lock Wait

Deadlocks

(10)

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 the

(11)

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

(12)

Problems if there is no concurrency control

• Lost update

• Uncommitted read

• Non-repeatable read

(13)

13 © 2011 IBM Corporation

Lost update

seat

name

...

7C

_____

7B

_____

...

reservations

(14)

Lost update

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

seat

name

...

7C

_____

7B

_____

...

reservations

(15)

15 © 2011 IBM Corporation

Lost update

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

seat

name

...

7C

_____

7B

_____

...

reservations

John

(16)

Lost update

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

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

seat

name

...

7C

_____

7B

_____

...

reservations

John

(17)

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

John Mary

(18)

Lost update

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

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

seat

name

...

7C

_____

7B

_____

...

reservations

John Mary

App A App B

(19)

19 © 2011 IBM Corporation

Uncommitted read (also known as “dirty read”)

seat

name

...

7C

_____

7B

_____

...

reservations

(20)

Uncommitted read (also known as “dirty read”)

seat

name

...

7C

_____

7B

_____

...

reservations

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

(21)

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

(22)

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

(23)

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'

(24)

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

(25)

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

(26)

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

Further processing in App B uses incorrect /

(27)

27 © 2011 IBM Corporation

Non-repeatable read

seat

name

...

7C

_____

7B

_____

...

reservations

(28)

Non-repeatable read

seat

name

...

7C

_____

7B

_____

...

reservations

select seat

from reservations where name is NULL

(29)

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

(30)

Non-repeatable read

seat

name

...

7C

_____

7B

_____

...

reservations

select seat

from reservations where name is NULL

App A App B

7C

7B

(31)

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

(32)

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

(33)

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 NULL

(34)

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

7B

The same SELECT (read) returns a different result: Less rows (in this case '7C' doesn't show anymore).

(35)

35 © 2011 IBM Corporation

Phantom read

seat

name

...

7C

_____

7B

_____

...

reservations

Susan

(36)

Phantom read

seat

name

...

7C

_____

7B

_____

...

reservations

select seat

from reservations where name is NULL

Susan

(37)

37 © 2011 IBM Corporation

Phantom read

seat

name

...

7C

_____

7B

_____

...

reservations

select seat

from reservations where name is NULL

Susan

App A App B

(38)

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'

(39)

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'

(40)

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

(41)

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

(42)

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

...

select seat

from reservations

7B

The same SELECT (read) returns a different result: More rows (phantom

rows, in this case '7C', is shown)

The same SELECT (read) returns a different result: More rows (phantom

(43)

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)

(44)

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

(45)

45 © 2011 IBM Corporation

(46)

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

(47)

47 © 2011 IBM Corporation

Cursor stability with currently committed (CC) semantics

Cursor stability

without

currently committed

seat

name

...

7C

Susan

7B

_____

...

reservations

App A

App B

Cursor stability

with

currently committed (Default behavior)

seat

name

...

7C

Susan

7B

_____

...

reservations

(48)

Cursor stability with currently committed (CC) semantics

Cursor stability

without

currently committed

seat

name

...

7C

Susan

7B

_____

...

reservations

App A

App B

Cursor stability

with

currently committed (Default behavior)

seat

name

...

7C

Susan

7B

_____

...

reservations

App A

App B

X

update

reservations

(49)

49 © 2011 IBM Corporation

Cursor stability with currently committed (CC) semantics

Cursor stability

without

currently committed

seat

name

...

7C

Susan

7B

_____

...

reservations

App A

App B

Cursor stability

with

currently committed (Default behavior)

seat

name

...

7C

Susan

7B

_____

...

reservations

App A

App B

X

update

reservations

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

(50)

Cursor stability with currently committed (CC) semantics

Cursor stability

without

currently committed

seat

name

...

7C

Susan

7B

_____

...

reservations

App A

App B

X

update

reservations

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

select name

from reservations where seat = '7C'

S

Cursor stability

with

currently committed (Default behavior)

seat

name

...

7C

Susan

7B

_____

...

reservations

App A

App B

(51)

51 © 2011 IBM Corporation

Cursor stability with currently committed (CC) semantics

Cursor stability

without

currently committed

seat

name

...

7C

Susan

7B

_____

...

reservations

App A

App B

X

update

reservations

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

select name

from reservations where seat = '7C'

S

Cursor stability

with

currently committed (Default behavior)

seat

name

...

7C

Susan

7B

_____

...

reservations

App A

App B

John

(52)

Cursor stability with currently committed (CC) semantics

Cursor stability

without

currently committed

seat

name

...

7C

Susan

7B

_____

...

reservations

App A

App B

X

update

reservations

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

select name

from reservations where seat = '7C'

S

Cursor stability

with

currently committed (Default behavior)

seat

name

...

7C

Susan

7B

_____

...

reservations

App A

App B

John

Lock Wait

(53)

53 © 2011 IBM Corporation

Cursor stability with currently committed (CC) semantics

Cursor stability

without

currently committed

seat

name

...

7C

Susan

7B

_____

...

reservations

App A

App B

X

update

reservations

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

select name

from reservations where seat = '7C'

S

Cursor stability

with

currently committed (Default behavior)

seat

name

...

7C

Susan

7B

_____

...

reservations

App A

App B

John

Lock Wait

App B hangs until App A commits or rolls back which releases X lock

X

update

reservations

(54)

Cursor stability with currently committed (CC) semantics

Cursor stability

without

currently committed

seat

name

...

7C

Susan

7B

_____

...

reservations

App A

App B

X

update

reservations

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

select name

from reservations where seat = '7C'

S

Cursor stability

with

currently committed (Default behavior)

seat

name

...

7C

Susan

7B

_____

...

reservations

App A

App B

John

Lock Wait

App B hangs until App A commits or rolls back which releases X lock

X

update

reservations

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

(55)

55 © 2011 IBM Corporation

Cursor stability with currently committed (CC) semantics

Cursor stability

without

currently committed

seat

name

...

7C

Susan

7B

_____

...

reservations

App A

App B

X

update

reservations

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

select name

from reservations where seat = '7C'

S

Cursor stability

with

currently committed (Default behavior)

seat

name

...

7C

Susan

7B

_____

...

reservations

App A

App B

John

Lock Wait

App B hangs until App A commits or rolls back which releases X lock

X

update

reservations

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

John

select name
(56)

Cursor stability with currently committed (CC) semantics

Cursor stability

without

currently committed

seat

name

...

7C

Susan

7B

_____

...

reservations

App A

App B

X

update

reservations

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

select name

from reservations where seat = '7C'

S

Cursor stability

with

currently committed (Default behavior)

seat

name

...

7C

Susan

7B

_____

...

reservations

App A

App B

John

Lock Wait

App B hangs until App A commits or rolls back which releases X lock

X

update

reservations

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

John

select name
(57)

57 © 2011 IBM Corporation

Comparing and choosing an isolation level

Isolation Level Lost

update

Dirty Read

Non-repeatable Read

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

(58)

Transactions

Concurrency & Locking

• Lock Wait

Deadlocks

(59)

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):

seat

name

...

7C

Susan

7B

_____

...

reservations

(60)

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):

seat

name

...

7C

Susan

7B

_____

...

reservations

App A

App B

X

update

reservations

(61)

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):

seat

name

...

7C

Susan

7B

_____

...

reservations

App A

App B

X

update

reservations

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

(62)

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):

seat

name

...

7C

Susan

7B

_____

...

reservations

App A

App B

X

update

reservations

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

John

select name
(63)

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):

seat

name

...

7C

Susan

7B

_____

...

reservations

App A

App B

X

update

reservations

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

John

select name

from reservations where seat = '7C'

S

(64)

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):

seat

name

...

7C

Susan

7B

_____

...

reservations

App A

App B

X

update

reservations

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

select name

from reservations where seat = '7C'

S

John

(65)

65 © 2011 IBM Corporation

Transactions

Concurrency & Locking

Lock Wait

• Deadlocks

(66)

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 B

(67)

67 © 2011 IBM Corporation

Deadlocks

seat

name

...

7C

Susan

7B

_____

...

8E

Raul

9F

Jin

App A

App B

X

update

reservations

set 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

(68)

Deadlocks

seat

name

...

7C

Susan

7B

_____

...

8E

Raul

9F

Jin

App A

App B

X

update

reservations

set 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

(69)

69 © 2011 IBM Corporation

Deadlocks

seat

name

...

7C

Susan

7B

_____

...

8E

Raul

9F

Jin

App A

App B

X

update

reservations

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

reservations

John

update reservations

set 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

(70)

Deadlocks

seat

name

...

7C

Susan

7B

_____

...

8E

Raul

9F

Jin

App A

App B

X

update

reservations

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

reservations

John

update reservations

set name = 'Sue' where seat = '9F'

X

Sue

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

(71)

71 © 2011 IBM Corporation

Deadlocks

seat

name

...

7C

Susan

7B

_____

...

8E

Raul

9F

Jin

App A

App B

X

update

reservations

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

reservations

John

update reservations

set name = 'Sue' where seat = '9F'

X

Sue

S

... select name from reservations where 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

(72)

Deadlocks

seat

name

...

7C

Susan

7B

_____

...

8E

Raul

9F

Jin

App A

App B

X

update

reservations

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

reservations

John

update reservations

set name = 'Sue' where seat = '9F'

X

Sue

S

... select name from reservations where seat = '9F'

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

(73)

73 © 2011 IBM Corporation

Deadlocks

seat

name

...

7C

Susan

7B

_____

...

8E

Raul

9F

Jin

App A

App B

X

update

reservations

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

reservations

John

update reservations

set name = 'Sue' where seat = '9F'

X

Sue

S

... select name from reservations where seat = '9F'

Lock Wait

...

select name

from reservations where 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

(74)

Deadlocks

seat

name

...

7C

Susan

7B

_____

...

8E

Raul

9F

Jin

App A

App B

X

update

reservations

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

reservations

John

update reservations

set name = 'Sue' where seat = '9F'

X

Sue

S

... select name from reservations where seat = '9F'

Lock Wait

...

select name

from reservations where 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

(75)

75 © 2011 IBM Corporation

Deadlocks

seat

name

...

7C

Susan

7B

_____

...

8E

Raul

9F

Jin

App A

App B

X

update

reservations

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

...

select name

from reservations where seat = '7C'

S

John

S

... select name from reservations where seat = '9F'

update

reservations set name = 'Sue' where seat = '9F'

X

Sue

reservations Lock Wait 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

(76)

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.

(77)

77 © 2011 IBM Corporation

Referensi

Dokumen terkait

mengadakan koordinasi dengan penyedia jasa pelatihan lainnya (baik swasta maupun negeri), termasuk: (i) guru (perorangan) dari sekolah lain di kabupaten/kota

Pirngadi Medan adalah rumah sakit milik pemerintah yang bertujuan meningkatkan pelayanan kepada masyarakat dalam rangka memajukan kesejahteraan umum dengan fleksibilitas

Untuk menuliskan tipe data ini cukup dengan klik cell yang akan diisi data, kemudian ketikkan angka akan ditampilkan.. Penulisan karaktek koma(dalam format Indonesia) atau

Google dengan layanan Google Drive memfasilitasi penggunanya untuk berkolaborasi, membuat, menyimpan dan membagi dokumen dengan pengguna lainnya.. Fitur dan layanan Google

Membandingkan hasil koreksi kelainan refraksi pada pemeriksaan subjektif ( trial and error ) dengan pemeriksaan objektif (streak retinoskopi) tanpa sikloplegik pada

mutu dalam jangka waktu yang ditentukan maka PPK akan menghitung biaya perbaikan yang diperlukan, dan PPK secara langsung atau melalui pihak ketiga yang ditunjuk oleh PPK akan

The explicit teaching of the extent of reaction from the beginning of the equilibrium treatment permits the unification of the use of ICE (Initial, Change, Equilibrium) tables

Kasus 2: Nilai ujian akhir anda di atas 80 (hipotesis benar) tetapi anda tidak mendapat nilai A (konklusi salah)..  dosen berbohong