0% found this document useful (0 votes)
30 views6 pages

Deadlock Concurrency Indexes

Uploaded by

Alieu Keita
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views6 pages

Deadlock Concurrency Indexes

Uploaded by

Alieu Keita
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

DEADLOCK IN DBMS

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.

o Circular Waiting: It is when two or more transactions wait each other indefinitely for a lock
held by the others to be released.

o Partial Allocation/Hold & Wait: When a transaction acquires some of the required data
items but not all the data items as they may be exclusively locked by others.

o Non-Preemptive scheduling: A data item that could be only single transaction at a time.

o Mutual Exclusion: A data item can be locked exclusively by one transaction at a time.

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.

Wait for Graph

o This is the suitable method for deadlock detection. In this method, a graph is created based
on the transaction and their lock. If the created graph has a cycle or closed loop, then there
is a deadlock.

o The wait for the graph is maintained by the system for every transaction which is waiting for
some data held by the others. The system keeps checking the graph if there is any cycle in
the graph.

Components of a Wait-For Graph:

1. Nodes:

o Each node in the graph represents a transaction.

2. Edges:

o A directed edge Ti→TjT_i \rightarrow T_jTi→Tj exists if transaction TiT_iTi is waiting


for transaction TjT_jTj to release a resource (e.g., a lock on a database item).

Deadlock Detection Using Wait-For Graph:


• A deadlock occurs when there is a cycle in the wait-for graph.

• If a cycle is detected in the graph, it means that the transactions involved in the cycle are
waiting on each other in a circular dependency, and none of them can proceed, resulting in a
deadlock.

Example of Wait-For Graph:

1. Scenario:

o Transaction T1T_1T1 holds a lock on Resource A and is waiting for a lock on Resource
B.

o Transaction T2T_2T2 holds a lock on Resource B and is waiting for a lock on Resource
A.

2. Wait-For Graph:

o T1→T2T_1 \rightarrow T_2T1→T2 (Transaction T1T_1T1 is waiting for T2T_2T2).

o T2→T1T_2 \rightarrow T_1T2→T1 (Transaction T2T_2T2 is waiting for T1T_1T1).

3. Cycle:

o The graph has a cycle T1→T2→T1T_1 \rightarrow T_2 \rightarrow T_1T1→T2→T1,


indicating a deadlock.

CONCURRENCY CONTROL

Concurrency control is a mechanism in database management systems (DBMS) that manages the
simultaneous execution of multiple transactions to ensure that database operations are performed
in a manner that maintains data consistency, integrity, and isolation. It prevents potential issues
that arise when multiple transactions access shared resources or modify data concurrently.

Concurrency control in databases is essential to ensure data consistency, integrity, and reliability
when multiple users or transactions access the database simultaneously. Without proper
concurrency control mechanisms, several problems can arise, compromising the accuracy and
usability of the database.

Key Reasons for Concurrency Control:

1. Maintaining Consistency:

o Multiple transactions running concurrently may read or modify data in a way that
leads to inconsistent database states. Concurrency control ensures that all
transactions leave the database in a consistent state.

2. Preventing Data Anomalies:

o Lost Updates: When two transactions update the same data simultaneously, one
update may overwrite the other without knowledge.
o Dirty Reads: A transaction reads data written by another uncommitted transaction,
which may later roll back, leading to invalid data.

o Non-Repeatable Reads: A transaction reads the same data multiple times but gets
different results due to updates by other transactions.

o Phantom Reads: A transaction re-executes a query and finds new rows due to inserts
by other concurrent transactions.

3. Ensuring Isolation:

o Transactions must be isolated from each other to ensure they don’t interfere.
Concurrency control enforces isolation levels, such as Serializable, Repeatable Read,
Read Committed, and Read Uncommitted, depending on the application's
requirements.

4. Preserving Atomicity:

o Transactions should be all-or-nothing units of work. Concurrency control ensures


partial updates of a failed transaction do not affect other concurrent transactions.

5. Enhancing System Performance:

o Efficient concurrency control mechanisms balance the need for isolation and
throughput, ensuring high system performance while minimizing conflicts.

6. Preventing Deadlocks and Resource Contention:

o Proper concurrency control helps detect and resolve deadlocks or avoid them
altogether, ensuring smooth transaction execution.

Methods for Concurrency Control:

• Lock-Based Protocols: Shared locks (for reads), exclusive locks (for writes), and strict two-
phase locking (2PL) to ensure serializability.

• Optimistic Concurrency Control (OCC): Transactions proceed without locks and validate
changes before committing to avoid conflicts.

• Timestamp Ordering: Transactions are ordered based on timestamps to prevent conflicts.

• Multiversion Concurrency Control (MVCC): Maintains multiple versions of data to allow


readers and writers to proceed concurrently.

By implementing concurrency control, databases ensure that simultaneous operations do not


negatively impact data integrity and application functionality.

2PL locking protocol (2 Phase Locking Protocol)

Every transaction will lock and unlock the data item in two different phases.

• Growing Phase − All the locks are issued in this phase. No locks are released, after all
changes to data-items are committed and then the second phase (shrinking phase) starts.
• Shrinking phase − No locks are issued in this phase, all the changes to data-items are noted
(stored) and then locks are released.

In the growing phase transaction reaches a point where all the locks it may need has been acquired.
This point is called LOCK POINT.

After the lock point has been reached, the transaction enters a shrinking phase.

TYPES:

Strict two phase locking protocol

A transaction can release a shared lock after the lock point, but it cannot release any exclusive lock
until the transaction commits. This protocol creates a cascade less schedule.

Cascading schedule: In this schedule one transaction is dependent on another transaction. So if one
has to rollback then the other has to rollback.

Rigorous two phase locking protocol

A transaction cannot release any lock either shared or exclusive until it commits.

The 2PL protocol guarantees serializability, but cannot guarantee that deadlock will not happen.
INDEXES

An Index is a lookup table associated with an actual table or View that is used by the database to
improve the data retrieval performance timing. In an index, keys are stored in a structure (B-tree)
that enables SQL Server to find the rows associated with the key values quickly and efficiently. Index
gets created automatically if the primary key and unique key constraints are defined on the table.
There are two types of index namely, Clustered Index and Non-Clustered Index.

Indexes are used in Oracle to provide quick access to rows in a table. Indexes provide faster access to
data for operations that return a small portion of a table's rows.

CREATE INDEX index_name

ON table_name (col1, col2);

What is a Clustered Index?

A Clustered index is a type of index where the table records are physically reordered to match with
the index. If a Table is created with primary key constraints, then the database engine automatically
creates a clustered index. In this, data is sorted or stored in the table or view based on their key and
values.

A clustered index contains the main data. It is quick, thus the data retrieval is faster in the clustered
index, and it also requires less memory space to perform operations. A clustered index has the
inherent ability to store data on the disc.

What is Non-Clustered Index?

Key Clustered Index Non-Clustered Index

Basic Clustered index is created on primary Non-clustered index can be created on any
key. key.

Ordering Store data physically according to the It doesn't impact the order.
order.

Number of Only one clustered index can be there There can be any number of non-clustered
index in a table. indexes in a table.

Space No extra space is required to store Extra space is required to store logical
logical structure. structure.

Performance Data retrieval is faster than non-cluster Data update is faster than clustered index.
index.

Leaf node The leaf nodes contains the actual The leaf nodes does not contain the actual
data in the clustered index. data in the non-clustered index.

Number of A table can have only one clustered A table can have multiple non clustered
indices index. indices.
Data Clustered index contains main data. Non-clustered index contains a copy of data.

A Non-clustered index is a special type of index in which the logical order of the index does not
match with the physically stored order of the rows on the disk. In this type of index, the table is
created with UNIQUE constraints, then the database engine automatically creates a non-clustered
index.

A non-clustered index contains the non-clustered index keyvalues and each keyvalue entry has a
pointer to the data row that contains the key value. However, a non-clustered index is relatively
slower, and it requires more memory space to perform the operations. Also, a non-clustered index
does not have the inherent property of storing data on the disc.

You might also like