Unit 5 Concurrency Control
Unit 5 Concurrency Control
Transaction A reads the value of data DT as 1000 and modifies it to 1500 which gets stored in the temporary buffer.
The transaction B reads the data DT as 1500 and commits it and the value of DT permanently gets changed to 1500 in
the database DB. Then some server errors occur in transaction A and it wants to get rollback to its initial value, i.e.,
1000 and then the dirty read problem occurs.
Unrepeatable Read Problem
The unrepeatable read problem occurs when two or more different values of the same data are read during the read
operations in the same transaction.
Example: Consider two transactions A and B performing read/write operations on a data DT in the database DB. The
current value of DT is 1000: The following table shows the read/write operations in A and B transactions.
Transaction A and B initially read the value of DT as 1000. Transaction A modifies the value of DT from 1000 to 1500
and then again transaction B reads the value and finds it to be 1500. Transaction B finds two different values of DT in
its two different read operations.
Transaction B initially reads the value of DT as 1000. Transaction A deletes the data DT from the database DB and
then again transaction B reads the value and finds an error saying the data DT does not exist in the database DB.
Concurrency Control is the working concept that is required for controlling and managing theconcurrent
execution of database operations and thus avoiding the inconsistencies in the database. Thus, for maintaining
the concurrency of the database, we have the 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:
o Lock Based Concurrency Control Protocol
o Time Stamp Concurrency Control Protocol
o Validation Based Concurrency Control Protocol
Lock-Based Protocol
In this type of protocol, any transaction cannot read or write data until it acquires anappropriate lock on it. There
are two types of lock:
1. Shared lock:
o It is also known as a Read-only lock. In a shared lock, the data item can only read bythe transaction.
o It can be shared between the transactions because when the transaction holds a lock,then it can't update the
data on the data item.
2. Exclusive lock:
o In the exclusive lock, the data item can be both reads as well as written by thetransaction.
o This lock is exclusive, and in this lock, multiple transactions do not modify the samedata simultaneously.
Example:
The following way shows how unlocking and locking work with 2-PL.
Transaction T1:
o Growing phase: from step 1-3
o Shrinking phase: from step 5-7
o Lock point: at 3
Transaction T2:
o Growing phase: from step 2-6
o Shrinking phase: from step 8-9
o Lock point: at 6
The two methods outlined below can be used to convert between the locks:
1. Conversion from a read lock to a write lock is an upgrade.
2. Conversion from a write lock to a read lock is a downgrade.
Types of Lock-Based Protocols
There are basically four lock based protocols in DBMS namely Simplistic Lock Protocol, Pre-claiming Lock
Protocol, Two-phase Locking Protocol, and Strict Two-Phase Locking Protocol. Let's go through each of these
lock-based protocols in detail.
Simplistic Lock Protocol
The simplistic method is defined as the most fundamental method of securing data during a transaction. Simple
lock-based protocols allow all transactions to lock the data before inserting, deleting, or updating it. After the
transaction is completed, the data item will be unlocked.
Pre-Claiming Lock Protocol
Pre-claiming Lock Protocols are known to assess transactions to determine which data elements require locks. Prior
to actually starting the transaction, it asks the Database management system for all of the locks on all of the data
items. The pre-claiming protocol permits the transaction to commence if all of the locks are obtained. Whenever
the transaction is finished, the lock is released. This protocol permits the transaction to roll back if all of the locks
are not granted and then waits until all of the locks are granted.
The transaction table is shown in the example associated with transaction T1 and transaction T2. It represents the
schedule produced using validation protocol.
The concurrent transaction process starts with T1 with a reading operation as Read (A) where A is the numeric data
element in the database. In the next step, the transaction T2 also reads the same data variable A after some time.
Transaction T2 performs an arithmetic operation by subtracting constant value 40 from the variable A. It is represented
as A=A-40 in the transaction table. The next step is a read operation on transaction T2 where it’s reading another
numerical value of variable B as the Read (B). After the read operation completed, the transaction T2 immediately
performs an arithmetic operation on the variable B. It uses the addition operator ‘+’ for adding a constant value as 80
to variable B. The addition operation is represented as B=B+80 in the transaction table.
In the next step of the concurrent transaction, T1 reads the variable B with operation Read (B). Now the validation
based protocol comes into the action in the transaction T1 that validates the time stamp of the start phase of T2 lesser
than the finishing phase time stamp of Transaction T1 and that is a lesser timestamp as the validate phase of Transaction
T2.
Similarly, in the Transaction T2, the validation based protocol validates the timestamps. In the example shown in the
table indicates both the validation based protocol is provided with a valid result based on the timestamp condition. And,
as the conclusive operations write operations are performed by the transaction T2 using Write (A) and Write (B)
statements.
A deadlock is a condition where two or more transactions are waiting indefinitely for one another to give
up locks. Deadlock is said to be one of the most feared complications in DBMS as no task ever gets
finished and is in waiting state forever.
For example: In the student table, transaction T1 holds a lock on some rows and needs to update some
rows in the grade table. Simultaneously, transaction T2 holds locks on some rows in the grade table and
needs to update the rows in the Student table held by Transaction T1.
Now, the main problem arises. Now Transaction T1 is waiting for T2 to release its lock and similarly,
transaction T2 is waiting for T1 to release its lock. All activities come to a halt state and remain at a
standstill. It will remain in a standstill until the DBMS detects the deadlock and aborts one of the
transactions.
1. Deadlock Avoidance
o When a database is stuck in a deadlock state, then it is better to avoid the database rather than
aborting or restating the database. This is a waste of time and resource.
o Deadlock avoidance mechanism is used to detect any deadlock situation in advance. A method
like "wait for graph" is used for detecting the deadlock situation but this method is suitable only
for the smaller database. For the larger database, deadlock prevention method can be used.
2. Deadlock Detection
In a database, when a transaction waits indefinitely to obtain a lock, then the DBMS should detect
whether the transaction is involved in a deadlock or not. The lock manager maintains a Wait for the
graph to detect the deadlock cycle in the database.
The wait for a graph for the above scenario is shown below:
3. Deadlock Prevention
o Deadlock prevention method is suitable for a large database. If the resources are allocated in such
a way that deadlock never occurs, then the deadlock can be prevented.
o The Database management system analyzes the operations of the transaction whether they can
create a deadlock situation or not. If they do, then the DBMS never allowed that transaction to
be executed.
Wait-Die scheme
In this scheme, if a transaction requests for a resource which is already held with a conflicting lock by
another transaction then the DBMS simply checks the timestamp of both transactions. It allows the older
transaction to wait until the resource is available for execution.
Let's assume there are two transactions Ti and Tj and let TS(T) is a timestamp of any transaction T. If
T2 holds a lock by some other transaction and T1 is requesting for resources held by T2 then the
following actions are performed by DBMS:
1. Check if TS(Ti) < TS(Tj) - If Ti is the older transaction and Tj has held some resource, then Ti is
allowed to wait until the data-item is available for execution. That means if the older transaction
is waiting for a resource which is locked by the younger transaction, then the older transaction is
allowed to wait for resource until it is available.
2. Check if TS(Ti) < TS(Tj) - If Ti is older transaction and has held some resource and if Tj is
waiting for it, then Tj is killed and restarted later with the random delay but with the same
timestamp.
Selection of victim determine which transaction has to be rollback to break the deadlock.
That transaction should be rollback which occurs at minimum cost.
2. Rollback
Rollback determine how far this transaction should be roll back.
Total Rollback
Partial Rollback
3. Starvation
Some transaction never complete its designated work- thus there is a starvation.
What is deadlock? Explain wait-for-graph. When it occurs?
OR
Define deadlock. Explain wait-for-graph. Explain different conditions that lead
to deadlock.
Deadlock-A deadlock is a condition when two or more transactions are executing and each transaction is
waiting for the other to finish but none of them are ever finished. So all the transactions will wait for infinite
time and not a single transaction is completed.
Wait-for-graph
Table 1
Held by Wait for
Transaction 1 Transaction 2
R1 R3 R1 R3
P1 P2 P3 P1 P2 P3
R2 R4 R2 R4
No cycle (circular chain) Cycle (circular chain) created (P2, R3, P3, R4, P2) So
created So no deadlock deadlock
When a deadlock is detected, the system must recover from the deadlock.
The most common solution is to roll back one or more transactions to break the deadlock. Choosing which
transaction to abort is known as Victim Selection.
In the above wait-for graph transactions R3 and R4 are deadlocked.
In order to remove deadlock one of the transaction out of these two (R3, R4) transactions must be roll backed.
We should rollback those transactions that will incur the minimum cost.
When a deadlock is detected, the choice of which transaction to abort can be made using following criteria:
The transaction which have the fewest locks the transaction that has done the least work the transaction that
is farthest from completion
Explain deadlock prevention methods.
OR
Explain methods to prevent deadlock.
Deadlock prevention
A protocols ensure that the system will never enter into a deadlock state. Some prevention
strategies :
Require that each transaction locks all its data items before it begins
execution (predeclaration).
Impose partial ordering of all data items and require that a transaction
canlock data items only in the order specified by the partial.
Following schemes use transaction timestamps for the sake of deadlock
prevention alone.
1. Wait-die scheme — non-preemptive
• If an older transaction is requesting a resource which is held by younger
transaction, then older transaction is allowed to wait for it till it is available.
• If an younger transaction is requesting a resource which is held by older
transaction, then younger transaction is killed.
2. Wound-wait scheme — preemptive
• If an older transaction is requesting a resource which is held by younger
transaction, then older transaction forces younger transaction to kill the
transaction and release the resource.
• If an younger transaction is requesting a resource which is held by older
transaction, then younger transaction is allowed to wait till older
transaction will releases it.
3. Timeout-Based Schemes :
• A transaction waits for a lock only for a specified amount of time. After
that, the wait times out and the transaction is rolled back. So deadlocks
never occur.
• Simple to implement; but difficult to determine good value of the timeout
interval.
Database Failure/ Failure classification in DBMS
Failure in terms of a database can be defined as its inability to execute the specified
transaction or loss of data from the database.
A DBMS is vulnerable to several kinds of failures and each of these failures needs to be
managed differently.
There are many reasons that can cause database failures such as network failure, system
crash, natural disasters, carelessness, sabotage(corrupting the data intentionally), software
errors, etc.
To find that where the problem has occurred, we generalize a failure into the following
categories:
1. Transaction failure
2. System crash
3. Disk failure/Data Transfer Failure
1. Transaction failure
The transaction failure occurs when it fails to execute or when it reaches a point from
where it can't go any further. If a few transaction or process is hurt, then this is called
as transaction failure.
2. System Crash
o System failure can occur due to power failure or other hardware or software
failure. Example: Operating system error.
When a system crashes, it may have several transactions being executed and various files opened for them to
modify the data items.
But according to ACID properties of DBMS, atomicity of transactions as a whole must be maintained, that is,
either all the operations are executed or none.
Database recovery means recovering the data when it get deleted, hacked ordamaged accidentally.
Atomicity is must whether is transaction is over or not it should reflect in the databasepermanently or it should
not effect the database at all.
It should check the states of all the transactions, which were being executed.
A transaction may be in the middle of some operation; the DBMS must ensurethe atomicity of the transaction
in this case.
It should check whether the transaction can be completed now or it needs to berolled back.
No transactions would be allowed to leave the DBMS in an inconsistent state.
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 storagebefore 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
Schedules managed using timestamp based protocol are serializable just like the two phase protocols
Since older transactions are given priority which means no transaction has to wait for longer period of time
that makes this protocol free from deadlock.
Concurrency control means that multiple transactions can be executed at the same time and then the interleaved
logs occur. But there may be changes in transaction results so maintain the order of execution of those
transactions.
During recovery, it would be very difficult for the recovery system to backtrack all the logs and then start
recovering.
Recovery with concurrent transactions can be done in the following four ways.
Transaction rollback :
In this scheme, we rollback a failed transaction by using the log.
The system scans the log backward a failed transaction, for every log record found inthe log the system
restores the data item.
Checkpoints :
A checkpoint is a process of saving a snapshot of the applications state so that it canrestart from that point in
case of failure.
Checkpoint is a point of time at which a record is written onto the database form thebuffers.
Checkpoint shortens the recovery process.
When it reaches the checkpoint, then the transaction will be updated into the database, and till that point, the
entire log file will be removed from the file. Then the log file is updated with the new step of transaction till the
next checkpoint and so on.
The checkpoint is used to declare the point before which the DBMS was in the consistent state, and all the
transactions were committed.
Restart recovery:
When the system recovers from a crash, it constructs two lists.
The undo-list consists of transactions to be undone, and the redo-list consists oftransaction to be redone.
The system constructs the two lists as follows: Initially, they are both empty. Thesystem scans the log
backward, examining each record, until it finds the first
<checkpoint> record.
Check Points:
Checkpoints are a process of saving a snapshot of the applications state so that it canrestart from that point in
case of failure.
Checkpoint is a point of time at which a record is written onto the database form the buffers.
Checkpoint shortens the recovery process.
When it reaches the checkpoint, then the transaction will be updated into the database, and till that point, the
entire log file will be removed from the file. Then the log file is updated with the new step of transaction till the
next checkpoint and so on.
The checkpoint is used to declare the point before which the DBMS was in the consistent state, and all the
transactions were committed.
Explain system recovery procedure with Checkpoint record concept.
Time Tc Tf
T1
T2
T3
T4
Shadow Paging is recovery technique that is used to recover database. In this recovery
technique, database is considered as made up of fixed size of logical units of storage which
are referred as pages.
pages are mapped into physical blocks of storage, with help of the page table which allow
one entry for each logical page of database.
This method uses two page tables named current page table and shadow page table.
The entries which are present in current page table are used to point to most recent database
pages on disk.
Another table i.e., Shadow page table is used when the transaction starts which is copying
current page table.
After this, shadow page table gets saved on disk and current page table is going to be used
for transaction.
Entries present in current page table may be changed during execution but in shadow page
table it never get changed. After transaction, both tables become identical.
Advantages
No overhead of maintaining transaction log. Recovery is quite faster, as there is no any redo or
undo operations required.
Disadvantages
Copying the entire page table very expensive. Data are scattered or fragmented. After each
transaction, free pages need to be collected by garbage collector. Difficult to extend this
technique to allow concurrent transaction