SlideShare a Scribd company logo
6
Most read
7
Most read
8
Most read
1
Concurrency Control Techniques
Concurrency Control Techniques: Overview of Locking, 2PL, Timestamp ordering,
multiversioning, validation
There are number of concurrency control techniques that are used to ensure the isolation property
of concurrently executing transactions. Most of these techniques ensure serializability of
schedules using protocols (sets of rules) that guarantee serializability. One important set of
protocols employs the technique of locking data items to prevent multiple transactions from
accessing the items concurrently. Locking protocols are used in most commercial DBMSs.
Another set of concurrency control protocols use timestamps. A timestamp is a unique identifier
for each transaction, generated by the system.
The multiversion concurrency control protocols use multiple versions of a data item.
A protocol based on the concept of validation or certification of a transaction after it executes
its operations; is sometimes called optimistic protocol.
1. Locking Techniques for Concurrency Control
Transaction processing system usually allows multiple transactions to run concurrently. By
allowing multiple transactions to run concurrently will improve the performance of the system in
terms of increased throughout or improved response time, but this allows causes several
complications with consistency of the data. Ensuring consistency in spite of concurrent execution
of transaction require extra work, which is performed by the concurrency controller system of
DBMS.
What is Lock?
A lock is a variable associated with a data item that describes the status of the item with respect
to possible operations that can be applied to it. Generally, there is one lock for each data item in
the database. Locks are used as a means of synchronizing the access by concurrent transactions
to the database item.
Types of Locks
Generally 2 types of locks are used in concurrency control.
i) Binary locks, which are simple but restrictive and so are not used in practice.
ii) Shared/Exclusive locks, which provide more general locking capabilities and are used
in practical database locking schemes.
i) Binary Locks
A binary lock can have two states or values: locked and unlocked. A distinct lock is associated
with each database item A. If the value of the lock on A is 1, item A cannot be accessed by a
2
database operation that requests the item. If the value of the lock on A is 0 then item can be
accessed when requested.
We refer to the current value of the lock associated with item A as LOCK (A).
There are two operations, lock_item(A) and unlock(A) that are used with binary locking. A
transaction requests access to an item A by first issuing a lock_item(A) operation.
 If LOCK (A) = 1, the transaction is forced to wait.
 If LOCK (A) = 0 it is set to 1 (the transaction locks the item) and the transaction is
allowed to access item A.
When the transaction is through using the item, it issues an unlock(A) operation, which sets
LOCK (A) to 0 (unlocks the item) so that A may be accessed by other transactions. Hence binary
lock enforces mutual exclusion on the data item.
Rules of Binary Locks
If the simple binary locking scheme described here is used, every transaction must obey the
following rules:
1) A transaction must issue the operation lock_item(A) before any read_item (A) or write,
item operations are performed in T.
2) A transaction T must issue the operation unlock(A) after all read_item (A) and
write_item (A) operations are completed in T.
3) A transaction T will not issue a lock _item (A) operation if it already holds the lock on
Item A.
4) A transaction T will not issue an unlock(A) operation unless it already holds the lock on
item A.
5) The lock manager module of the DBMS can enforce these rules. Between the lock_item
(A) and unlock(A) operations in transaction T, is said to hold the lock on item A. At most
one transaction can hold the lock on a particular item. Thus no two transactions can
access the same item concurrently.
Disadvantages of Binary Locks
As binary locking scheme is too restrictive for database items, because at most one transaction
can hold a lock on a given item. So, binary locking system cannot be used for practical purpose.
ii) Share/Exclusive (for Read/Write) Locks
Locking operations: There are three locking operations called read_lock(A), write_lock(A)
and unlock(A). A lock associated with an item A, LOCK (A), now has three possible states:
"read-locked", "write-locked," or "unlocked." A read-locked item is also called share-locked item
because other transactions are allowed to read the item, whereas a write-locked item is caused
exclusive-locked, because a single transaction exclusively holds the lock on the item.
3
Compatibility of Locks Suppose that there are A and B two different locking modes Read or
Shared(S) & Write or Exclusive(X). If a transaction T1 requests a lock of mode on item Q on
which transaction T2 currently hold a lock of mode B. If transaction can be granted lock, in spite
of the presence of the mode B lock, then we say mode A is compatible with mode B. Such a
function is shown in one matrix as shown below:
The graphs shows that if two transactions only read the same data object they do not conf1ict,
but if one transaction writes a data object and another either read or write the same data object,
then they conflict with each other. A transaction requests a shared lock on data item Q by
executing the read_lock(Q) instruction. Similarly, an exclusive lock is requested through the
write_lock(Q) instruction. A data item Q can be unlocked via the unlock(Q) instruction.
How Should Lock be Used?
To access a data item, transaction T1 must first lock that item. If the data item is already locked
by another transaction in an incompatible mode, the concurrency control manager will not grant
the lock until all incompatible locks held by other transactions have been released. Thus, T1 is
made to wait until all incompatible locks held by other transactions have been released
When we use the shared/exclusive locking scheme, the system must enforce the following rules:
a. A transaction T must issue the operation read_lock(X) or write_lock(X) before
any read_item(X) operation is performed in T.
b. A transaction T must issue the operation write_lock(X) before any write_item(X)
operation is performed in T.
c. A transaction T must issue the operation unlock(X) after all read_item(X) and
write_item(X) operations are completed in T.
d. 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.
e. 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 be relaxed.
f. A transaction T will not issue an unlock(X) operation unless it already holds a
read (shared) lock or a write (exclusive) lock on item X.
4
For example, a transaction T1’ after applying the read/write or shared/exclusive lock on T1.
T1 T1’(equivalent transaction after applying read/write lock)
Example: Let x & y are two accounts that are accessed by transactions T1 & T2. T1 transfers
50/- from account x to y and is defined below.
Transaction T1 T2: simply add x & y and display the sum
Suppose that the values of accounts x and y are Rs.200 and Rs.100, respectively. If these two
transactions are executed serially, either in the order T1 and T2 or the order T2 and T1 then
transaction T2 will display the value Rs.300. If however, these transactions are executed
concurrently, as shown in Schedule 1, in this case, transaction T2 displays Rs.250, which is
incorrect. The reason for this mistake is that the transaction TI unlocked data item x too early, as
a result of which T2 shows an inconsistent state.
read(x);
read(y);
y=y+x;
write(y);
read_lock(x); Shared lock on x
read(x);
unlock(x); unlock x
write_lock(y); exclusive lock on y
read(y);
y = y + x;
write(y);
unlock(y); unlock y
read_lock(y);
read(y);
unlock(y);
read_lock(x);
read(x);
unlock(x);
sum=x+y;
display(sum);
write_lock(x);
read(x);
x = x – 50;
write(x);
unlock(x);
write_lock(y);
read(y);
y = y + 50;
write(y);
unlock(y);
5
T1 T2
write_lock(x);
read(x);
x = x – 50;
write(x);
unlock(x);
read_lock(y);
read(y);
unlock(y);
read_lock(x);
read(x);
unlock(x);
sum=x+y;
display(sum);
write_lock(y);
read(y);
y = y + 50;
write(y);
unlock(y);
Figure: Schedule 1
Hence, it can be found that using read/write locking mechanism doesn’t guarantee serializibilty.
Guaranteeing serializibilty can be done by the locking scheme called 2-phase locking scheme or
2PL mechanism.
6
Two Phase Locking (2PL) Protocol
The use of locks has helped us to create neat and clean concurrent schedule. The Two Phase
Locking Protocol defines the rules of how to acquire the locks on a data item and how to release
the locks. The Two Phase Locking Protocol assumes that a transaction can only be in one of two
phases.
i) Growing Phase: During this phase, the transaction can only acquire locks but no existing lock
can be released.
ii) Shrinking Phase: During this phase, existing locks can be released but no new locks can be
acquired.
The 2PC Locking protocol always results in a serializable schedule.
Example :: here the transaction T1 do not follow the 2PC Protocol, because the write_lock(y)
operation in T1 follows the unlock(x) operation. On enforcing the 2PC Locking scheme, the
transaction T1 can be written as T1’ as follows:
T1 T1’
Problems in Two-Phase Locking
 Deadlock - It happens whenever a transaction waits for a lock to be unlock (to access the
data).
 Cascading roll back -When the data requested by 1 transaction is held by some other
transactions again and again and the requested data is not given.
write_lock(x);
read(x);
x = x – 50;
write(x);
unlock(x);
write_lock(y);
read(y);
y = y + 50;
write(y);
unlock(y);
write_lock(x);
read(x);
x = x – 50;
write(x);
write_lock(y);
unlock(x);
read(y);
y = y + 50;
write(y);
unlock(y);
7
Concurrency Control Based on Timestamp Ordering
The Timestamp Ordering(TO) Algorithm
The timestamp-ordering protocol ensures serializability among transaction in their conflicting
read and write operations. This is the responsibility of the protocol system that the conflicting
pair of tasks should be executed according to the timestamp values of the transactions.
Timestamps
A timestamp is a unique identifier created by the DBMS to identify a transaction. Typically,
timestamp values are assigned in the order in which the transactions are submitted to the system,
so a timestamp can be thought of as the transaction start time. The timestamp of transaction T
can be referred as TS(T). Concurrency control techniques based on timestamp ordering do not
use locks; hence, deadlocks cannot occur.
In this, a unique fixed timestamp is associated with each transaction to keep the order of the
transaction. It is denoted by TS (Ti).
Example: If a transaction T1 has been assigned timestamp TS (T1) and a new transaction TS
(T2) enters the system, then TS (T1) < TS (T2).
Two methods for implementing TS
 Use the value of the system as the timestamp (System Clock).
 Use a logical counter that is incremented after a new timestamp has been assigned.
Implementation Method
The timestamp of a data item can be of the following two types:
 WTS(Q) or W-timestamp (Q): This means the latest time when the data item Q has
been written into.
 RTS(Q) or R-timestamp (Q): This means the latest time when the data item Q has been
read from.
These two timestamps are updated each time a successful read/write operation is performed on
the data item Q.
Timestamp ordering protocol works as follows:
1. If a transaction Ti issues read operation:
If TS(Ti) < WTS(Q), then the read operation is rejected and the transaction Ti is restarted
with a new timestamp value; otherwise, i.e if TS(Ti) >= WTS(Q) , the read operation is executed
and RTS(Q) is set to max(RTS(Q), TS(Ti)).
2. If a transaction Ti issues write operation:
If TS (Ti) < RTS(Q) or TS (Ti) < WTS(Q), then the write operation is rejected or
rollback and the transaction Ti is restarted with a new timestamp value; otherwise, if TS(Ti) >=
RTS(Q) or TS(Ti) >= WTS(Q) , the write operation is executed and WTS(Q) is set to TS(Ti).
8
Example:
Step T1 T2 T3 a b c
(200) (150) (175) RT(a)=0, RT(b)=0, RT(c)=0,
WT(a)=0 WT(b)=0 WT(c)=0
1 Read(b) RT(b)=200
2 Read(a) RT(a)=150
3 Read(c) RT(c)=175
4 Write(b) WT(b)=200
5 Write(a) WT(a)=200
6 Write(c)
7 Write(a)
At step1, the operationread(b) executed,as
TS(T1) < WTS(b) i.e 200 < 0 does not follows , and
RTS(b) is set to max(RTS(b), TS(T1)) i.e RTS(b)=200
Similar rules for step 2, 3
For step 4 the operationwrite(b)executed,as
TS (T1) < RTS(b) or TS (T1) < WTS(b) i.e. 200 < 200 or 200 < 0 does not follows , and
WTS(b) is set to TS(T1) i.e WTS(b)=200
Step 5 the operationwrite(a)isalsoexecuted.
At step6, howeverthe operationwrite(c) isrejectedasitdoesnotfollowsthe rule
At step7, T3 triesto write a & the read value of a i.e RTS(a) =150. But TS(T3) > RT(a) , i.e 175 > 150, T3
neednotto be aborted.

More Related Content

PPTX
Concurrency control
PPTX
Distributed system architecture
PPT
Distributed Transaction
PPTX
deadlock handling
PPTX
Transactions and Concurrency Control
PPTX
Lock based protocols
PPT
16. Concurrency Control in DBMS
PPT
Two phase commit protocol in dbms
Concurrency control
Distributed system architecture
Distributed Transaction
deadlock handling
Transactions and Concurrency Control
Lock based protocols
16. Concurrency Control in DBMS
Two phase commit protocol in dbms

What's hot (20)

PDF
Schedule in DBMS
PPTX
Distributed DBMS - Unit 8 - Distributed Transaction Management & Concurrency ...
PPTX
Timestamp protocols
PPT
15. Transactions in DBMS
PPTX
Lec 7 query processing
PPTX
8 queens problem using back tracking
PPT
Np cooks theorem
PPT
Distributed file systems dfs
PPTX
Concurrency Control in Database Management System
PPTX
Distributed DBMS - Unit 9 - Distributed Deadlock & Recovery
PPT
Distributed objects & components of corba
PPTX
Overview of Concurrency Control & Recovery in Distributed Databases
PPTX
Concurrency control
PPTX
Query processing and optimization (updated)
PPT
Distributed data processing
PPTX
Distributed Query Processing
PPTX
Security in distributed systems
PPTX
Concurrency Control in Distributed Database.
PPTX
Distributed file system
PPTX
Transaction management DBMS
Schedule in DBMS
Distributed DBMS - Unit 8 - Distributed Transaction Management & Concurrency ...
Timestamp protocols
15. Transactions in DBMS
Lec 7 query processing
8 queens problem using back tracking
Np cooks theorem
Distributed file systems dfs
Concurrency Control in Database Management System
Distributed DBMS - Unit 9 - Distributed Deadlock & Recovery
Distributed objects & components of corba
Overview of Concurrency Control & Recovery in Distributed Databases
Concurrency control
Query processing and optimization (updated)
Distributed data processing
Distributed Query Processing
Security in distributed systems
Concurrency Control in Distributed Database.
Distributed file system
Transaction management DBMS
Ad

Similar to Concurrency Control Techniques (20)

PPT
Chapter18
PPT
concurrency-control
PDF
Module 5 part-2 concurrency control in dbms
PPTX
Concurrency Control in Databases.Database management systems
PPTX
Module-5_DataBase Management System.pptx
PPTX
CHapter four database managementm04.pptx
PPTX
Concurrency control!
PPTX
Chapter 4-Concrruncy controling techniques.pptx
PDF
PPT-concurrency Control database management system DBMS concurrent control (U...
PPTX
concurrency control
PPT
Chapter Three _Concurrency Control Techniques_ETU.ppt
PPTX
DBMS Pravin concurrency control technique.pptx
PPT
concurrency-control-techniques.ppt
PPTX
transaction management-database management system
PDF
Concurrency note.pdf
PPTX
DBMS Presentation.pptx
PPTX
Concurrency Control & Deadlock Handling
PDF
Unit 5 rdbms study_material
PDF
Cs501 concurrency
PPT
5-Chapter Five - Concurrenc.ppt
Chapter18
concurrency-control
Module 5 part-2 concurrency control in dbms
Concurrency Control in Databases.Database management systems
Module-5_DataBase Management System.pptx
CHapter four database managementm04.pptx
Concurrency control!
Chapter 4-Concrruncy controling techniques.pptx
PPT-concurrency Control database management system DBMS concurrent control (U...
concurrency control
Chapter Three _Concurrency Control Techniques_ETU.ppt
DBMS Pravin concurrency control technique.pptx
concurrency-control-techniques.ppt
transaction management-database management system
Concurrency note.pdf
DBMS Presentation.pptx
Concurrency Control & Deadlock Handling
Unit 5 rdbms study_material
Cs501 concurrency
5-Chapter Five - Concurrenc.ppt
Ad

More from Raj vardhan (20)

PPTX
Software Testing Life Cycle Unit-3
PPTX
Internet Basics Unit-7
PPTX
Local Area Network – Wired LAN
PPTX
Network Connecting Devices UNIT 5
DOCX
UNIT 4-HEADER FILES IN C
PPTX
Wireless LANs(IEEE802.11) Architecture
PPTX
UNIT -03 Transmission Media and Connecting Devices
PDF
Unit 1: Introduction to DBMS Unit 1 Complete
PPTX
Introduction To Software Concepts Unit 1 & 2
DOCX
Swachh Bharat Abhiyan - Project Report
DOCX
Network Topology
DOCX
Microsoft Office Word Introduction Complete
DOCX
Digital money Revolution Introduction
DOCX
C Programming
PPTX
Definition of Business
PPT
Business Terms & Concepts
PDF
Number System Conversion | BCA
DOCX
Interaction With Computers FIT
DOCX
FIT-MS-WORD Lab | BCA
PDF
Syllabus Front End Design Tool VB.NET | BCA-205
Software Testing Life Cycle Unit-3
Internet Basics Unit-7
Local Area Network – Wired LAN
Network Connecting Devices UNIT 5
UNIT 4-HEADER FILES IN C
Wireless LANs(IEEE802.11) Architecture
UNIT -03 Transmission Media and Connecting Devices
Unit 1: Introduction to DBMS Unit 1 Complete
Introduction To Software Concepts Unit 1 & 2
Swachh Bharat Abhiyan - Project Report
Network Topology
Microsoft Office Word Introduction Complete
Digital money Revolution Introduction
C Programming
Definition of Business
Business Terms & Concepts
Number System Conversion | BCA
Interaction With Computers FIT
FIT-MS-WORD Lab | BCA
Syllabus Front End Design Tool VB.NET | BCA-205

Recently uploaded (20)

PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Pre independence Education in Inndia.pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
GDM (1) (1).pptx small presentation for students
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
Institutional Correction lecture only . . .
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
Lesson notes of climatology university.
PDF
Basic Mud Logging Guide for educational purpose
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
master seminar digital applications in india
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Complications of Minimal Access Surgery at WLH
PDF
Insiders guide to clinical Medicine.pdf
PDF
01-Introduction-to-Information-Management.pdf
Supply Chain Operations Speaking Notes -ICLT Program
Pre independence Education in Inndia.pdf
human mycosis Human fungal infections are called human mycosis..pptx
GDM (1) (1).pptx small presentation for students
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
Institutional Correction lecture only . . .
Module 4: Burden of Disease Tutorial Slides S2 2025
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Lesson notes of climatology university.
Basic Mud Logging Guide for educational purpose
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
master seminar digital applications in india
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Final Presentation General Medicine 03-08-2024.pptx
Complications of Minimal Access Surgery at WLH
Insiders guide to clinical Medicine.pdf
01-Introduction-to-Information-Management.pdf

Concurrency Control Techniques

  • 1. 1 Concurrency Control Techniques Concurrency Control Techniques: Overview of Locking, 2PL, Timestamp ordering, multiversioning, validation There are number of concurrency control techniques that are used to ensure the isolation property of concurrently executing transactions. Most of these techniques ensure serializability of schedules using protocols (sets of rules) that guarantee serializability. One important set of protocols employs the technique of locking data items to prevent multiple transactions from accessing the items concurrently. Locking protocols are used in most commercial DBMSs. Another set of concurrency control protocols use timestamps. A timestamp is a unique identifier for each transaction, generated by the system. The multiversion concurrency control protocols use multiple versions of a data item. A protocol based on the concept of validation or certification of a transaction after it executes its operations; is sometimes called optimistic protocol. 1. Locking Techniques for Concurrency Control Transaction processing system usually allows multiple transactions to run concurrently. By allowing multiple transactions to run concurrently will improve the performance of the system in terms of increased throughout or improved response time, but this allows causes several complications with consistency of the data. Ensuring consistency in spite of concurrent execution of transaction require extra work, which is performed by the concurrency controller system of DBMS. What is Lock? A lock is a variable associated with a data item that describes the status of the item with respect to possible operations that can be applied to it. Generally, there is one lock for each data item in the database. Locks are used as a means of synchronizing the access by concurrent transactions to the database item. Types of Locks Generally 2 types of locks are used in concurrency control. i) Binary locks, which are simple but restrictive and so are not used in practice. ii) Shared/Exclusive locks, which provide more general locking capabilities and are used in practical database locking schemes. i) Binary Locks A binary lock can have two states or values: locked and unlocked. A distinct lock is associated with each database item A. If the value of the lock on A is 1, item A cannot be accessed by a
  • 2. 2 database operation that requests the item. If the value of the lock on A is 0 then item can be accessed when requested. We refer to the current value of the lock associated with item A as LOCK (A). There are two operations, lock_item(A) and unlock(A) that are used with binary locking. A transaction requests access to an item A by first issuing a lock_item(A) operation.  If LOCK (A) = 1, the transaction is forced to wait.  If LOCK (A) = 0 it is set to 1 (the transaction locks the item) and the transaction is allowed to access item A. When the transaction is through using the item, it issues an unlock(A) operation, which sets LOCK (A) to 0 (unlocks the item) so that A may be accessed by other transactions. Hence binary lock enforces mutual exclusion on the data item. Rules of Binary Locks If the simple binary locking scheme described here is used, every transaction must obey the following rules: 1) A transaction must issue the operation lock_item(A) before any read_item (A) or write, item operations are performed in T. 2) A transaction T must issue the operation unlock(A) after all read_item (A) and write_item (A) operations are completed in T. 3) A transaction T will not issue a lock _item (A) operation if it already holds the lock on Item A. 4) A transaction T will not issue an unlock(A) operation unless it already holds the lock on item A. 5) The lock manager module of the DBMS can enforce these rules. Between the lock_item (A) and unlock(A) operations in transaction T, is said to hold the lock on item A. At most one transaction can hold the lock on a particular item. Thus no two transactions can access the same item concurrently. Disadvantages of Binary Locks As binary locking scheme is too restrictive for database items, because at most one transaction can hold a lock on a given item. So, binary locking system cannot be used for practical purpose. ii) Share/Exclusive (for Read/Write) Locks Locking operations: There are three locking operations called read_lock(A), write_lock(A) and unlock(A). A lock associated with an item A, LOCK (A), now has three possible states: "read-locked", "write-locked," or "unlocked." A read-locked item is also called share-locked item because other transactions are allowed to read the item, whereas a write-locked item is caused exclusive-locked, because a single transaction exclusively holds the lock on the item.
  • 3. 3 Compatibility of Locks Suppose that there are A and B two different locking modes Read or Shared(S) & Write or Exclusive(X). If a transaction T1 requests a lock of mode on item Q on which transaction T2 currently hold a lock of mode B. If transaction can be granted lock, in spite of the presence of the mode B lock, then we say mode A is compatible with mode B. Such a function is shown in one matrix as shown below: The graphs shows that if two transactions only read the same data object they do not conf1ict, but if one transaction writes a data object and another either read or write the same data object, then they conflict with each other. A transaction requests a shared lock on data item Q by executing the read_lock(Q) instruction. Similarly, an exclusive lock is requested through the write_lock(Q) instruction. A data item Q can be unlocked via the unlock(Q) instruction. How Should Lock be Used? To access a data item, transaction T1 must first lock that item. If the data item is already locked by another transaction in an incompatible mode, the concurrency control manager will not grant the lock until all incompatible locks held by other transactions have been released. Thus, T1 is made to wait until all incompatible locks held by other transactions have been released When we use the shared/exclusive locking scheme, the system must enforce the following rules: a. A transaction T must issue the operation read_lock(X) or write_lock(X) before any read_item(X) operation is performed in T. b. A transaction T must issue the operation write_lock(X) before any write_item(X) operation is performed in T. c. A transaction T must issue the operation unlock(X) after all read_item(X) and write_item(X) operations are completed in T. d. 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. e. 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 be relaxed. f. A transaction T will not issue an unlock(X) operation unless it already holds a read (shared) lock or a write (exclusive) lock on item X.
  • 4. 4 For example, a transaction T1’ after applying the read/write or shared/exclusive lock on T1. T1 T1’(equivalent transaction after applying read/write lock) Example: Let x & y are two accounts that are accessed by transactions T1 & T2. T1 transfers 50/- from account x to y and is defined below. Transaction T1 T2: simply add x & y and display the sum Suppose that the values of accounts x and y are Rs.200 and Rs.100, respectively. If these two transactions are executed serially, either in the order T1 and T2 or the order T2 and T1 then transaction T2 will display the value Rs.300. If however, these transactions are executed concurrently, as shown in Schedule 1, in this case, transaction T2 displays Rs.250, which is incorrect. The reason for this mistake is that the transaction TI unlocked data item x too early, as a result of which T2 shows an inconsistent state. read(x); read(y); y=y+x; write(y); read_lock(x); Shared lock on x read(x); unlock(x); unlock x write_lock(y); exclusive lock on y read(y); y = y + x; write(y); unlock(y); unlock y read_lock(y); read(y); unlock(y); read_lock(x); read(x); unlock(x); sum=x+y; display(sum); write_lock(x); read(x); x = x – 50; write(x); unlock(x); write_lock(y); read(y); y = y + 50; write(y); unlock(y);
  • 5. 5 T1 T2 write_lock(x); read(x); x = x – 50; write(x); unlock(x); read_lock(y); read(y); unlock(y); read_lock(x); read(x); unlock(x); sum=x+y; display(sum); write_lock(y); read(y); y = y + 50; write(y); unlock(y); Figure: Schedule 1 Hence, it can be found that using read/write locking mechanism doesn’t guarantee serializibilty. Guaranteeing serializibilty can be done by the locking scheme called 2-phase locking scheme or 2PL mechanism.
  • 6. 6 Two Phase Locking (2PL) Protocol The use of locks has helped us to create neat and clean concurrent schedule. The Two Phase Locking Protocol defines the rules of how to acquire the locks on a data item and how to release the locks. The Two Phase Locking Protocol assumes that a transaction can only be in one of two phases. i) Growing Phase: During this phase, the transaction can only acquire locks but no existing lock can be released. ii) Shrinking Phase: During this phase, existing locks can be released but no new locks can be acquired. The 2PC Locking protocol always results in a serializable schedule. Example :: here the transaction T1 do not follow the 2PC Protocol, because the write_lock(y) operation in T1 follows the unlock(x) operation. On enforcing the 2PC Locking scheme, the transaction T1 can be written as T1’ as follows: T1 T1’ Problems in Two-Phase Locking  Deadlock - It happens whenever a transaction waits for a lock to be unlock (to access the data).  Cascading roll back -When the data requested by 1 transaction is held by some other transactions again and again and the requested data is not given. write_lock(x); read(x); x = x – 50; write(x); unlock(x); write_lock(y); read(y); y = y + 50; write(y); unlock(y); write_lock(x); read(x); x = x – 50; write(x); write_lock(y); unlock(x); read(y); y = y + 50; write(y); unlock(y);
  • 7. 7 Concurrency Control Based on Timestamp Ordering The Timestamp Ordering(TO) Algorithm The timestamp-ordering protocol ensures serializability among transaction in their conflicting read and write operations. This is the responsibility of the protocol system that the conflicting pair of tasks should be executed according to the timestamp values of the transactions. Timestamps A timestamp is a unique identifier created by the DBMS to identify a transaction. Typically, timestamp values are assigned in the order in which the transactions are submitted to the system, so a timestamp can be thought of as the transaction start time. The timestamp of transaction T can be referred as TS(T). Concurrency control techniques based on timestamp ordering do not use locks; hence, deadlocks cannot occur. In this, a unique fixed timestamp is associated with each transaction to keep the order of the transaction. It is denoted by TS (Ti). Example: If a transaction T1 has been assigned timestamp TS (T1) and a new transaction TS (T2) enters the system, then TS (T1) < TS (T2). Two methods for implementing TS  Use the value of the system as the timestamp (System Clock).  Use a logical counter that is incremented after a new timestamp has been assigned. Implementation Method The timestamp of a data item can be of the following two types:  WTS(Q) or W-timestamp (Q): This means the latest time when the data item Q has been written into.  RTS(Q) or R-timestamp (Q): This means the latest time when the data item Q has been read from. These two timestamps are updated each time a successful read/write operation is performed on the data item Q. Timestamp ordering protocol works as follows: 1. If a transaction Ti issues read operation: If TS(Ti) < WTS(Q), then the read operation is rejected and the transaction Ti is restarted with a new timestamp value; otherwise, i.e if TS(Ti) >= WTS(Q) , the read operation is executed and RTS(Q) is set to max(RTS(Q), TS(Ti)). 2. If a transaction Ti issues write operation: If TS (Ti) < RTS(Q) or TS (Ti) < WTS(Q), then the write operation is rejected or rollback and the transaction Ti is restarted with a new timestamp value; otherwise, if TS(Ti) >= RTS(Q) or TS(Ti) >= WTS(Q) , the write operation is executed and WTS(Q) is set to TS(Ti).
  • 8. 8 Example: Step T1 T2 T3 a b c (200) (150) (175) RT(a)=0, RT(b)=0, RT(c)=0, WT(a)=0 WT(b)=0 WT(c)=0 1 Read(b) RT(b)=200 2 Read(a) RT(a)=150 3 Read(c) RT(c)=175 4 Write(b) WT(b)=200 5 Write(a) WT(a)=200 6 Write(c) 7 Write(a) At step1, the operationread(b) executed,as TS(T1) < WTS(b) i.e 200 < 0 does not follows , and RTS(b) is set to max(RTS(b), TS(T1)) i.e RTS(b)=200 Similar rules for step 2, 3 For step 4 the operationwrite(b)executed,as TS (T1) < RTS(b) or TS (T1) < WTS(b) i.e. 200 < 200 or 200 < 0 does not follows , and WTS(b) is set to TS(T1) i.e WTS(b)=200 Step 5 the operationwrite(a)isalsoexecuted. At step6, howeverthe operationwrite(c) isrejectedasitdoesnotfollowsthe rule At step7, T3 triesto write a & the read value of a i.e RTS(a) =150. But TS(T3) > RT(a) , i.e 175 > 150, T3 neednotto be aborted.