Unit-5 - TBT
Unit-5 - TBT
CSE)
by
Anubhav Sharma
Asstt. Professor
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Anubhav Sharma (A.P. CSE)
Concurrency control is the procedure in DBMS for managing simultaneous operations without conflicting with each
another.
Concurrent access is quite easy if all users are just reading data. There is no way they can interfere with one
another.
But, it would have a mix of READ and WRITE operations and hence the concurrency is a challenge.
Concurrency control is used to address such conflicts which mostly occur with a multi-user system. It helps us to
make sure that database transactions are performed concurrently without violating the data integrity of respective
databases.
Therefore, concurrency control is a most important element for the proper functioning of a system where two or
multiple database transactions that require access to the same data, are executed simultaneously.
Concurrency-control protocols : allow concurrent schedules, but ensure that the schedules are conflict/view
serializable, and are recoverable and maybe even cascadeless.
• These protocols do not examine the precedence graph as it is being created, instead a protocol imposes a
discipline that avoids non-seralizable schedules.
• Different concurrency control protocols provide different advantages between the amount of concurrency they
allow and the amount of overhead that they impose.
Anubhav Sharma (A.P. CSE)
Different concurrency control protocols offer different benefits between the amount of
concurrency they allow and the amount of overhead that they impose.
Lock-Based Protocols
Timestamp-Based Protocols
Validation-Based Protocols
Multiple Granularity Protocols, etc.,
Anubhav Sharma (A.P. CSE)
LOCK-BASED PROTOCOL:
In this type of protocol, any transaction cannot read or write data until it acquires an appropriate lock on it.
There are two types of locks:
1. Shared lock(S):
It is also known as a Read-only lock. In a shared lock, the data item can only read by the transaction.
It can be shared between the transactions because when the transaction holds a lock, then it will never have permission
to update data on the data item.
2. Exclusive lock(X):
In the exclusive lock, the data item can be both reads as well as written by the transaction.
This lock is exclusive, and in this lock, multiple transactions do not modify the same data simultaneously.
Note: Any number of transactions can hold shared locks on an item, but if any transaction holds an exclusive(X) lock on
the item no other transaction may hold any lock on the item.
Upgrade / Downgrade locks : A transaction that holds a lock on an item A is allowed under certain condition to change
the lock state from one state to another.
Upgrade: A S(A) can be upgraded to X(A) if Ti is the only transaction holding the S-lock on element A.
Downgrade: We may downgrade X(A) to S(A) when we feel that we no longer want to write on data-item A. As we were
holding X-lock on A, we need not check any conditions.
Anubhav Sharma (A.P. CSE)
LOCK-BASED PROTOCOL:
There are four types of lock based protocols,
1. Simplistic lock protocol
2. Pre-claiming Lock Protocol
3. Two-phase locking (2PL)
4. Strict Two-phase locking (Strict-2PL)
Starvation
Starvation is the situation when a transaction needs to wait for an indefinite period to acquire a lock.
Following are the reasons for Starvation:
• When waiting scheme for locked items is not properly managed
• In the case of resource leak
• The same transaction is selected as a victim repeatedly
Deadlock
Deadlock refers to a specific situation where two or more processes are waiting for each other to release a resource or
more than two processes are waiting for the resources in a circular chain.
LOCK BASED PROTOCOLS Continued… Anubhav Sharma (A.P. CSE)
In the below example, if lock conversion is allowed then the following phase can happen:
1.Upgrading of lock (from S(a) to X (a)) is allowed in growing phase.
2.Downgrading of lock (from X(a) to S(a)) must be done in shrinking phase.
Note:
• It is true that the 2PL protocol offers serializability.
• However, it does not ensure that deadlocks do not happen.
LOCK BASED PROTOCOLS Continued… Anubhav Sharma (A.P. CSE)
The first phase of Strict-2PL is similar to 2PL. In the first phase, after acquiring all the locks, the transaction
continues to execute normally.
The only difference between 2PL and strict 2PL is that Strict-2PL does not release a lock after using it.
Strict-2PL waits until the whole transaction to commit, and then it releases all the locks at a time.
Strict-2PL protocol does not have shrinking phase of lock release.
Example:
Suppose there are there transactions T1, T2, and T3.
T1 has entered the system at time 0010
T2 has entered the system at 0020
T3 has entered the system at 0030
Priority will be given to transaction T1, then transaction T2 and lastly Transaction T3.
Advantages:
•Schedules are serializable just like 2PL protocols
•No waiting for the transaction, which eliminates the possibility of deadlocks
TIMESTAMP BASED PROTOCOL Continued… Anubhav Sharma (A.P. CSE)
The main idea for this protocol is to order the transactions based on their Timestamps. A schedule in which the
transactions participate is then serializable and the only equivalent serial schedule permitted has the transactions
in the order of their Timestamp Values.
Timestamp Based Algorithm must ensure that, for each items accessed by Conflicting Operations in the schedule,
the order in which the item is accessed does not violate the ordering. To ensure this, use two Timestamp Values
relating to each database item X.
W_TS(X) is the largest timestamp of any transaction that executed write(X) successfully.
R_TS(X) is the largest timestamp of any transaction that executed read(X) successfully.
Validation phase is also known as optimistic concurrency control technique. In the validation based protocol, the
transaction is executed in the following three phases:
1. Read phase:
In this phase, the transaction T is read and executed. It is used to read the value of various data items and stores them in
temporary local variables. It can perform all the write operations on temporary variables without an update to the actual
database.
2. Validation phase:
In this phase, the temporary variable value will be validated against the actual data to see if it violates the serializability.
3. Write phase:
If the validation of the transaction is validated, then the temporary results are written to the database or system
otherwise the transaction is rolled back.
Multiple Granularity:
• It can be defined as hierarchically breaking up the database into blocks which can be locked.
• The Multiple Granularity protocol enhances concurrency and reduces lock overhead.
• It maintains the track of what to lock and how to lock.
• It makes easy to decide either to lock a data item or to unlock a data item. This type of hierarchy can be graphically
represented as a tree.
For example:
Consider a tree which has four levels of nodes.
• The first level or higher level shows the entire database.
• The second level represents a node of type area. The higher level database consists of exactly these areas.
• The area consists of children nodes which are known as files. No file can be present in more than one area.
• Finally, each file contains child nodes known as records. The file has exactly those records that are its child nodes. No
records represent in more than one file.
• Hence, the levels of the tree starting from the top level are as follows:
1.Database
2.Area
3.File
4.Record
Anubhav Sharma (A.P. CSE)
MULTIPLE GRANULARITY PROTOCOL Continued…
For example, if transaction T igets an explicit lock on
file Fc in exclusive mode, then it has an implicit lock in
exclusive mode on all the records belonging to that file.
It does not need to lock the individual records of
Fc explicitly. this is the main difference between Tree
Based Locking and Hierarchical locking for multiple
granularity.
In this example, the highest level shows the entire database. The levels below are file, record, and fields.
MULTIPLE GRANULARITY PROTOCOL Continued… Anubhav Sharma (A.P. CSE)
There are two types of techniques, which can help a DBMS in recovering as well as maintaining the atomicity of a transaction,
Maintaining the logs of each transaction, and writing them onto some stable storage before actually modifying the database.
Maintaining shadow paging, where the changes are done on a volatile memory, and later, the actual database is updated.
LOG BASED RECOVERY: Anubhav Sharma (A.P. CSE)
Log is a sequence of records, which maintains the records of actions performed by a transaction. It is important that the
logs are written prior to the actual modification and stored on a stable storage media, which is failsafe(it can be recovered
from there).
If any operation is performed on the database, then it will be recorded in the log.
But the process of storing the logs should be done before the actual transaction is applied in the database.
• Undo: using a log record sets the data item specified in log record to old value.
• Redo: using a log record sets the data item specified in log record to new value.
• The recovery system reads the logs backwards from the end to the last checkpoint.
• It maintains two lists, an undo-list and a redo-list.
• If the recovery system sees a log with <Tn, Start> and <Tn, Commit>, it puts the transaction in the redo-list.
• If the recovery system sees a log with <Tn, Start> but no commit or abort log found, it puts the transaction in undo-list.
All the transactions in the undo-list are then undone and their logs are removed. All the transactions in the redo-list and their previous logs
are removed and then redone before saving their logs.