Deadlock Concurrency Indexes
Deadlock Concurrency Indexes
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.
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.
1. Nodes:
2. Edges:
• 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.
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:
3. Cycle:
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.
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.
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 Efficient concurrency control mechanisms balance the need for isolation and
throughput, ensuring high system performance while minimizing conflicts.
o Proper concurrency control helps detect and resolve deadlocks or avoid them
altogether, ensuring smooth transaction execution.
• 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.
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:
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.
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.
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.
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.