DBMS M5 PPT
DBMS M5 PPT
Prof.Navyashree K S
Assistant Professor
Dept.of CSE (Data Science)
CONCURRENCY CONTROL
Introduction:
Concurrent execution refers to the simultaneous execution of more than one
transaction. This is a common scenario in multi-user database environments where
many users or applications might be accessing or modifying the database at the same
time.
Advantages of Concurrent Execution
1.Increased System Throughput: Multiple transactions can be in progress at the same
time, but at different stages
2.Maximized Processor Utilization: If one transaction is waiting for I/O operations,
another transaction can utilize the processor.
3.Decreased Wait Time: Transactions no longer have to wait for other long
transactions to complete.
4.Improved Transaction Response Time: Transactions get processed faster because
they can be executed in parallel.
Concurrency Control Protocols :The concurrency control protocols ensure
the atomicity, consistency, isolation, durability and serializability of the
concurrent execution of the database transactions.
Therefore, these protocols are categorized as:
1. Lock Based Concurrency Control Protocol
2. Time Stamp Concurrency Control Protocol
3. Validation Based Concurrency Control Protocol
• If we enforce two-phase locking, the transactions can be rewritten as T1 and T2 , as shown in Figure 22.4. Now, the schedule shown in
Figure 22.3(c) is not permitted for T1 and T2 (with their modified order of locking and unlocking operations) under the rules of locking
described in Section 22.1.1 because T1 will issue its write_lock(X) before it unlocks item Y; consequently, when T2 issues
its read_lock(X), it is forced to wait until T1 releases the lock by issuing an unlock (X) in the schedule.
• 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. The locking protocol, by enforcing two-phase locking rules, also
enforces serializability.
Two-phase locking may limit the amount of concurrency that can occur in a schedule
because a transaction T may not be able to release an item X after it is through using it
if T must lock an additional item Y later; or conversely, T must lock the additional
item Y before it needs it so that it can release X.
Hence, X must remain locked by T until all items that the transaction needs to read or
write have been locked; only then can X be released by T.
Meanwhile, another transaction seeking to access X may be forced to wait, even
though T is done with X; conversely, if Y is locked earlier than it is needed, another
transaction seeking to access Y is forced to wait even though T is not using Y yet.
This is the price for guaranteeing serializability of all schedules without having to
check the schedules themselves.
Although the two-phase locking protocol guarantees serializability (that is, every
schedule that is permitted is serializable), it does not permit all possible serializable
schedules (that is, some serializable schedules will be prohibited by the protocol).
Basic, Conservative, Strict, and Rigorous Two-Phase Locking.
There are a number of variations of two-phase locking (2PL). The technique just described is known as basic
2PL.
1. A variation known as conservative 2PL (or static 2PL) requires a transaction to lock all the items it accesses before the
transaction begins execution, by predeclaring its read-set and write-set. Recall from Section 21.1.2 that the read-set of a
transaction is the set of all items that the transaction reads, and the write-set is the set of all items that it writes.
• 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.
• Conservative 2PL is a deadlock-free protocol, we can discuss the deadlock problem. However, it is difficult to use in
practice because of the need to predeclare the read-set and write-set, which is not possible in many situations.
2. In practice, the most popular variation of 2PL is strict 2PL, which guarantees strict .
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. Strict 2PL is not deadlock-free.
3.A more restrictive variation of strict 2PL is 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.
Notice the difference between conservative and rigorous 2PL: the former 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 after it terminates (by
committing or aborting), so the transaction is in its expanding phase until it ends.
In many cases, the concurrency control subsystem itself is responsible for generating
the read_lock and write_lock requests.
• For example, suppose the system is to enforce the strict 2PL protocol. Then, whenever
transaction T issues a read_item(X), the system calls the read_lock(X) operation on behalf of T.
• If the state of LOCK(X) is write_locked by some other transaction T , the system places T in the
waiting queue for item X; otherwise, it grants the read_lock(X) request and permits the read_item(X)
operation of T to execute.
• On the other hand, if transaction T issues a write_item(X), the system calls the write_lock(X)
operation on behalf of T. If the state of LOCK(X) is write_locked or read_locked by some other
transaction T , the system places T in the waiting queue for item X;
• if the state of LOCK(X) is read_locked and T itself is the only transaction holding the read lock
on X, the system upgrades the lock to write_locked and permits the write_item(X) operation by T.
• Finally, if the state of LOCK(X) is unlocked, the system grants the write_lock(X) request and
permits the write_item(X) operation to execute. After each action, the system must update its lock
table appropriately.
• The use of locks can cause two additional problems: deadlock and starvation.
3. Dealing with Deadlock and Starvation
Deadlock occurs when each transaction T in a set of two or more transactions is waiting for some item that
is locked by some other transaction T in the set. Hence, each transaction in the set is in a waiting queue,
waiting for one of the other trans-actions in the set to release the lock on an item. But because the other
transaction is also waiting, it will never release the lock.
• A simple example is shown in Figure 22.5(a), where the two transactions T1 and T2 are deadlocked in a
partial schedule; T1 is in the waiting queue for X, which is locked by T2 , while T2 is in the waiting queue
for Y, which is locked by T1 . Meanwhile, neither T1 nor T2 nor any other transaction can access
items X and Y.
Deadlock Prevention
• A Transaction Locks all the data items it refers to before it begins execution.
• This way of locking prevents the deadlock since a transaction never waits for a data item.
• The conservative two phase locking uses this type of approach.
Deadlock detection and resolution
In this approach , deadlocks are allowed to happen. The scheduler maintains a wait for
graph for detecting cycle. If a cycle exists, then one of the transaction involved in the cycle
is selected as victim and roll back.
A wait for graph is created using lock table. As soon as a transaction is blocked , it is added
to the graph. When a chain like : Ti waits for Tj waits for Tk waits for Ti or Tj occurs , then
this creates a cycle.
DeadLock avoidance
There are many variations of two phase locking algorithm. Some avoid deadlock by not
letting cycle to complete.That is as soon as the algorithm discovers that blocking a
transaction is likely to create a cycle , it rolls back the transaction.
Dealing with deadlock & Starvation:
• Starvation occurs when a particular transaction consistently waits or restarted and
never gets a chance to proceed further.
• In a deadlock resolution it is possible that the same transaction may consistently be
selected as victim and roll back.
• This limitations in inherent in all priority based scheduling mechanisms.
• In wound wait scheme a younger transaction may always be wounded(aborted)by a
long running older transaction which creates starvation.