Adms CH-4
Adms CH-4
Chapter:4
❖Transaction uses locks to deny access to other transactions and so prevent incorrect
updates.
❖Lock prevents another transaction from modifying item or even reading it, in the
case of a write lock/ exclusive lock
A lock is a mechanism to control concurrent access to a data item
Locking is an operation which secures
(a) permission to Read
(b) permission to Write a data item for a transaction.
Notation :
:Li(X) –Transaction Ti requests a lock on database element X
• Unlocking is an operation which removes these permissions from
the data item.
• Notation :
• Ui (X): Transaction Ti releases (“unlocks”) its lock on
database element X
Lock and Unlock are Atomic operations.
Two-phase locking policy generates two locking algorithms
Basic
Conservative
Basic:
Transaction locks data items incrementally. This may cause
deadlock
Conservative:
Prevents deadlock by locking all desired data items before
transaction begins execution.
Cont.…
Types of a Lock
1. Shared lock/ Read lock
2. Exclusive lock/ write lock
Cont..
1. Shared lock/Read Lock: A Read operation does not change the value of
a data item. Hence a data item can be read by two different transactions
simultaneously under share lock mode. So only to read a data item
T1will do: Share lock (X), then Read (X), and finally Unlock (X
If transaction has a shared lock on an item, it can read but not update the item.
If a transaction has an exclusive lock on an item, it can both read and update the item.
Reads operation cannot form a conflict, so that more than one transaction can hold
shared locks simultaneously on same item.
Suppose a lock on a data item is applied, the data item is processed and
it is unlocked immediately after reading/writing is completed as
follows.
This is because although such kind of locking and unlocking data items
increases the concurrency of execution it violates the isolation and
atomicity properties of transactions.
Generally there are three types of lock protocols available for lock based concurrency control
methods:
Simplistic lock-based protocols allow transactions to obtain a lock on every object before a 'write'
operation is performed. Transactions may unlock the data item after completing the 'write' operation.
Lock item before access and unlock item when access is completed.
2. Pre-claiming protocols evaluate their operations and create a list of
data items on which they need locks. Before initiating an execution,
the transaction requests the system for all the locks it needs
beforehand.
If all the locks are granted, the transaction executes and releases all
the locks when all its operations are over.
If all the locks are not granted, the transaction rolls back and waits
until all the locks are granted
3. Two - Phase Locking - 2PL- protocol
In the first part, when the transaction starts executing, it seeks
permission for the locks it requires.
The second part is where the transaction acquires all the locks. As soon
as the transaction releases its first lock,
the third part starts. In this part, the transaction cannot demand any
new locks; it only releases the acquired locks
A transaction is said to follow two phase locking protocol if all
locking operations (either read_lock or write_lock) precede the first
unlock operation in the transaction
It can be proved that the transactions can be serialized in the order of
their lock points (i.e. the point where a transaction acquired its final
lock).
Two-phase locking has two phases,
growing phase, where all the locks are being acquired by the transaction
shrinking phase, where the locks held by the transaction are being released.
To claim an exclusive (write) lock, a transaction must first acquire a shared (read) lock and then upgrade it
to an exclusive lock
Problem of locking methods
1. Deadlock
Deadlock that may result when two (or more) transactions are each
waiting for locks held by the other to be released.
Thus, all the processes wait for each other to release resources. In this
situation, none of the processes can finish their task. This situation is
known as a deadlock.
The DBMS inspects the operations and analyzes if they can create a
deadlock situation. If it finds that a deadlock situation might occur,
then that transaction is never allowed to be executed.
There are deadlock prevention schemes that use timestamp
ordering mechanism of transactions in order to predetermine a
deadlock situation
Every transaction has a timestamp associated with it, and the ordering is
determined by the age of the transaction
Time Stamp Based Methods
A transaction created at 0002 clock time would be older than all other
transactions that come after it. For example, any transaction 'y'
entering the system at 0004 is two seconds younger and the priority
would be given to the older one.
In addition, every data item is given the latest read and write-
timestamp. This lets the system know when the last 'read and write'
operation was performed on the data item
Cont.…
The timestamp-ordering protocol ensures serializability among
transactions in their conflicting read and write operations.
This is the responsibility of the Time stamp ordering protocol that the
conflicting pair of tasks should be executed according to the
timestamp values of the transactions.
The timestamp of transaction Ti is denoted by TS(Ti).
Read timestamp of data-item X is denoted by R-timestamp(X).
Write timestamp of data-item X is denoted by W-timestamp(X)
Timestamp ordering protocol works as follows:
Operation rejected.
Operation executed.
Operation rejected.
In this technique, serializability is checked only at the time of commit and transactions
are aborted in case of non-serializable schedules
In this scheme, updates in the transaction are not applied directly to the database item
until it reaches its commit point
• Three phases:
1. Read phase
2. Validation phase
3. Write phase
1. Read phase:
• A transaction can read values of committed data items. However,
updates are applied only to local copies (versions) of the data
items (in database cache)