• No results found

Day 24 - Unit III - Concurrency Control – Locking Mechanisms- Two Phase Commit Protocol.ppt

N/A
N/A
Protected

Academic year: 2020

Share "Day 24 - Unit III - Concurrency Control – Locking Mechanisms- Two Phase Commit Protocol.ppt"

Copied!
17
0
0

Loading.... (view fulltext now)

Full text

(1)

Databases Concurrency Control

Purpose of Concurrency Control What is lock & unlock

Types of locking and system lock table binary locks & its rules

shared/exclusive lock(read/write lock) & its rules conversion of locks

Two-Phase locking(2PL) & guarantees serializability growing phase & shrinking phase

variation of two phase locking Basic Two-Phase Locking

Conservative Two-Phase Locking Strict Two-Phase Locking

(2)

Purpose of Concurrency Control

• To enforce Isolation (through mutual exclusion) among conflicting transactions.

• To preserve database consistency through consistency preserving execution of transactions.

• To resolve read-write and write-write conflicts.

(3)

Two-Phase Locking Techniques

Locking is an operation which secures (a) permission to Read or (b) permission to Write a data item for a transaction.

Example: Lock (X). Data item X is locked in behalf of the requesting transaction.

Unlocking is an operation which removes these permissions from the data item. Example: Unlock (X). Data item X is made available to all other transactions.

(4)

Binary Locks.

A binary lock can have two states or

values: locked and unlocked (or

1 and 0, for

simplicity).

A distinct lock is associated with each

database item

X.

If the

value of the lock on

X is 1, item X

cannot be accessed by a database operation that

requests the item.

If the value of the lock on

X is 0, the item can

be accessed when

requested, and the lock value is

changed to 1.

(5)
(6)

Rules for binary lock

If the simple binary locking scheme described here is

used, every transaction must obey the following rules:

1. A transaction T must issue the operation

lock_item(X) before any

read_item(

X) or

write_item(X) operations are performed in T.

2. A transaction T must issue the operation

unlock_item(X) after all read_item(X)

and

write_item(

X) operations are completed in T.

3. A transaction T will not issue a lock_item(X)

operation if it already holds the

lock on item

X.

(7)

Implementation of Binary Lock

It is quite simple to implement a binary lock;

all that is needed is a binary-valued variable,

LOCK, associated with each data item

X in the

database.

In its simplest

form, each lock can be a record

with three fields:

<Data_item_name, LOCK, Locking_transaction>

plus a queue for transactions that are waiting to

access the item.

The system needs to maintain

only these

records for the items that are currently locked

in a

(8)

Shared/Exclusive (or Read/Write) Locks.

The binary locking scheme is too restrictive for database items because at most, one transaction can hold a lock on a given item.

We should allow several transactions to access the same item

X if they all access X for reading purposes only.

This is because read operations on the same item by different transactions are not conflicting.

However, if a transaction is to write an item X, it must have exclusive access to X.

For this purpose, multiple-mode lock is used. shared/

exclusive or read/ write locks—there are three locking operations: read_lock(X), write_lock(X), and unlock(X).

A read-locked item is also called share-locked because other transactions are allowed to read the item, whereas a write-locked item is called exclusive-locked because a single transaction

(9)
(10)

Rules followed on shared/exclusive locking

1. A transaction T must issue the operation read_lock(X) or

write_lock(X) before any read_item(X) operation is performed in T.

2. A transaction T must issue the operation write_lock(X) before any write_item(X) operation is performed in T.

3. A transaction T must issue the operation unlock(X) after all read_item(X) and write_item(X) operations are completed in T.3

4. A transaction T will not issue a read_lock(X) operation if it already holds a read (shared) lock or a write (exclusive) lock on item X. This rule may be relaxed, as we discuss shortly.

5. A transaction T will not issue a write_lock(X) operation if it already holds a read (shared) lock or write (exclusive) lock on item X. This rule may also be relaxed, as we discuss shortly.

(11)

Lock conversion

Lock upgrade: existing read lock to write lock

if Ti has a read-lock (X) and Tj has no read-lock (X) (i

j)

then

convert read-lock (X) to write-lock (X)

else

force Ti to wait until Tj unlocks X

Lock downgrade: existing write lock to read lock

Ti has a write-lock (X) (*no transaction can

have any lock on X*)

(12)

Two-Phase Locking Techniques: The algorithm

Two Phases: (a) Locking (Growing) (b) Unlocking (Shrinking).

Locking (Growing) Phase: A transaction applies locks (read or write) on desired data items one at a time.

Unlocking (Shrinking) Phase: A transaction unlocks its locked data items one at a time.

Requirement: For a transaction these two phases must be mutually exclusively, that is, during locking phase unlocking phase must not start and during unlocking phase locking phase must not begin.

(13)

Example for transaction which do not obey Two-Phase Locking

Transactions T1 and T2 in Figure 22.3(a) do not follow the two-phase locking protocol because the write_lock(X) operation follows the unlock(Y) operation in T1,

(14)

How to make previous Phase locking into which obeying Two-Phase Locking (Basic Two-Two-Phase Locking) – not a dead lock free

If we enforce two-phase locking, the transactions can be rewritten as T1 and T2, as shown in Figure 22.4

It can be proved that, if every transaction in a schedule follows the two-phase locking protocol, the schedule is guaranteed to be serializable, obviating the need to test for serializability of schedules.

(15)

Variations of Two-Phase Locking

Conservative 2PL (or static 2PL)

requires a transaction to lock all the items it

accesses

before the transaction begins execution,

If any of the predeclared items needed

cannot be locked, the transaction does not lock any

item; instead, it waits until all the items are

available for locking.

(16)

Strict 2PL

which guarantees strict

schedules.

In this variation, a transaction

T does not

release any of

its exclusive (write) locks until

after

it commits or aborts.

Hence, no other transaction

can read or write

an item that is written by

T unless T has committed,

leading

to a strict schedule for recoverability.

(17)

Rigorous 2PL

which also guarantees strict schedules.

In this variation, a transaction

T does not release

any of its locks (exclusive or

shared) until after it

commits or aborts, and so it is easier to implement than

strict 2PL.

Note: the difference between conservative and

rigorous 2PL:

Conservative 2PL must lock all its items

before it

starts, so once the transaction starts it is in its

shrinking

phase;

the latter does not unlock any of its items until

References

Related documents

14 Represents the average number of software systems that are involved in the onboarding process – from applicant tracking systems … to HR and payroll

„ Left Rear Central Locking Lock Unit F222 and Right Rear Central Locking Lock Unit F223 function can be checked using Comfort System Central Control Module J393 On

- Logotype inclusion with link (directly to the sponsor website), on the WebPage of Congress, and in a zone on the website dedicated to Main Sponsors;. - Inclusion of a description

6-5555 for more detailed information on potential asbestos containing materials prior to any work that will disturb building materials such as sheetrock, plaster,

Presented to the highest of standards with five double bedrooms, three modern en suites, family bathroom, open plan kitchen/breakfast room, living room, office, cinema room,

• Amends state constitution to permit Legislature to exempt from property taxation the construction, installation, removal, or modification of all or any part of

Bryant (2016) Meteorological effects of the solar eclipse of 20 March 2015 - analysis of UK Met Office automatic weather station data and comparison with AWS data from the Faroes