Deadlocks, Concurrency Control, Recovery
Deadlocks, Concurrency Control, Recovery
Deadlocks, Concurrency Control, Recovery
DEADLOCKS
• A deadlock occurs when two transactions wait indefinitely for each other to unlock data.
• For Eg:, a deadlock occurs when two transactions, T1 and T2, exist in the following mode:
• T1 = access data items X and Y
• If T1 has not unlocked data item Y, T2 cannot begin; if T2 has not unlocked data item X, T1
cannot continue. Consequently, T1 and T2 each wait for the other to unlock the required
data item. Such a deadlock is also known as a deadly embrace.
2
DEADLOCKS
How a Deadlock condition is created? • The example used only two concurrent transactions
Lock Status to demonstrate a deadlock condition.
Time Transaction Reply
Data X Data Y
1 T1: LOCK(X) OK UNLOCKED UNLOCKED • In a real-world DBMS, many more transactions can be
2 T2: LOCK(Y) OK LOCKED UNLOCKED executed simultaneously, thereby increasing the
3 T1: LOCK(Y) WAIT LOCKED LOCKED
probability of generating deadlocks.
4 T2: LOCK(X) WAIT LOCKED LOCKED
5 T1: LOCK(Y) WAIT LOCKED LOCKED
• Deadlocks are possible only when one of the
6 T2: LOCK(X) WAIT LOCKED LOCKED
7 T1: LOCK(Y) WAIT LOCKED LOCKED transactions wants to obtain an exclusive lock on a
8 T2: LOCK(X) WAIT LOCKED LOCKED data item; no deadlock condition can exist among
9 T1: LOCK(Y) WAIT LOCKED LOCKED
10 T2: LOCK(X) WAIT LOCKED LOCKED shared locks
--- ---- ---- ---- ---- 3
DEADLOCK CONTROL METHODS
The three basic techniques to control deadlocks are:
2. Deadlock detection. The DBMS periodically tests the database for deadlocks. If a
deadlock is found, one of the transactions (the “victim”) is aborted (rolled back and
restarted) and the other transaction continues.
4
DEADLOCK CONTROL METHODS
3. Deadlock avoidance.
• Deadlock avoidance mechanism is used to detect any deadlock situation in advance.
• The transaction must obtain all of the locks it needs before it can be executed. This
technique avoids rolling back of conflicting transactions by requiring that locks be
obtained in succession.
• However, the serial lock assignment required in deadlock avoidance increases action
response times.
• A method like "wait for graph" is used for detecting the deadlock situation but this
method is suitable only for the smaller database.
5
DEADLOCK CONTROL METHODS
• The choice of the best deadlock control method to use depends on the database
environment.
• For example, if the probability of deadlocks is low, deadlock detection is
recommended. However, if the probability of deadlocks is high, deadlock
prevention is recommended. If response time is not high on the system’s
priority list, deadlock avoidance might be employed.
• All current DBMSs support deadlock detection in transactional databases, while
some DBMSs use a blend of prevention and avoidance techniques for other types
of data, such as data warehouses or XML data
6
CONCURRENCY CONTROL WITH TIME STAMPING METHODS
• The DBMS executes conflicting operations in time stamp order, thereby ensuring
serializability of the transactions.
• If two transactions conflict, one is stopped, rolled back, rescheduled, and assigned a
new time stamp value.
• The disadvantage of the time stamping approach is that each value stored in the database
requires two additional time stamp fields: one for the last time the field was read and
one for the last update. Time stamping thus increases memory needs and the
database’s processing overhead. Also, time stamping demands a lot of system resources
because many transactions might have to be stopped, rescheduled, and restamped.
8
WAIT/DIE & WOUND/WAIT SCHEMES
• Two schemes are used to decide which transaction is rolled back and which continues
executing: the wait/die scheme and the wound/wait scheme.
• Eg: Two conflicting transactions T1 and T2, each with a unique time stamp. Suppose T1 has a
time stamp of 115 and T2 has a time stamp of 195. From time stamps, it can be deduced that
T1 is the older transaction (lower time stamp). Given this scenario, 4 possible outcomes are:
TRANSACTION TRANSACTION
WAIT/DIE SCHEME WOUND/WAIT SCHEME
REQUESTING LOCK OWNING LOCK
• In both schemes, one of the transactions waits for the other transaction to finish
and release the locks. However, in many cases, a transaction requests multiple
locks.
• How long does a transaction have to wait for each lock request? Obviously, that
scenario can cause some transactions to wait indefinitely, causing a deadlock.
• To prevent that type of deadlock, each lock request has an associated time-out
value. If the lock is not granted before the time-out expires, the transaction is
rolled back. 11
CONCURRENCY CONTROL WITH OPTIMISTIC METHODS
• The optimistic approach is based on the assumption that the majority of the
database operations do not conflict.
• The optimistic approach requires neither locking nor time stamping techniques.
Instead, a transaction is executed without restrictions until it is committed.
• Using an optimistic approach, each transaction moves through two or three phases,
referred to as read, validation, and write.
• During the read phase, the transaction reads the database, executes the needed
computations, and makes the updates to a private copy of the database values. All
update operations of the transaction are recorded in a temporary update file, which
is not accessed by the remaining transactions.
• During the validation phase, the transaction is validated to ensure that the changes
made will not affect the integrity and consistency of the database. If the validation test
is positive, the transaction goes to the write phase. If the validation test is negative, the
transaction is restarted and the changes are discarded.
• During the write phase, the changes are permanently applied to the database.
13
DATABASE RECOVERY MANAGEMENT
Critical events can cause a database to become nonoperational and compromise the
integrity of the data. Examples of critical events are:
1. Hardware/software failures: hard disk media failure, application program or
operating system errors etc
2. Human-caused incidents: can be unintentional or intentional.
• Unintentional failure: caused by carelessness by end users like deleting the wrong
rows from table, pressing wrong key, or accidently shutting down the database server.
• Intentional events: security threats caused by hackers, virus attacks etc
3. Natural disasters: This category includes fires, earthquakes, floods, and power failures.
Whatever the cause, a critical error can render the database in an inconsistent state. 15
TRANSACTION RECOVERY
Database transaction recovery uses data in the transaction log to recover a database from an
inconsistent state to a consistent state.
1. The write-ahead-log protocol ensures that transaction logs are always written before
any database data are actually updated. This protocol ensures that, in case of a failure, the
database can later be recovered to a consistent state, using the data in the transaction log.
2. Redundant transaction logs (several copies of the transaction log) ensure that a
physical disk failure will not impair the DBMS’s ability to recover data
16
TRANSACTION RECOVERY - FOUR IMPORTANT CONCEPTS THAT AFFECT THE RECOVERY PROCESS
3. Database buffers are temporary storage areas in primary memory used to speed
up disk operations. When a transaction updates data, it updates the copy of the
data in the buffer because that process is much faster than accessing the physical
disk every time.
4. Database checkpoints are operations in which DBMS writes all of its updated
buffers to disk. A checkpoint operation is registered in the transaction log, thereby
physical database and transaction log will be in sync. This synchronization is
required because update operations update the copy in the buffers and not in the
physical database. 17
TRANSACTION RECOVERY PROCEDURES
19
TRANSACTION RECOVERY PROCEDURES
4. For any transaction that had a ROLLBACK operation after the last checkpoint, the
DBMS uses the transaction log records to ROLLBACK or undo the operations,
using the “before” values in the transaction log. Changes are applied in reverse
23