Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

Locking Mechanisms (Shared, Exclusive, Deadlock) in SQL


In SQL, locking mechanisms are used to ensure the integrity and consistency of the data in the database when multiple transactions are executed concurrently. Locking helps manage access to the same data by different transactions, preventing conflicts such as lost updates, inconsistent data, and other concurrency issues. The main types of locks in SQL are Shared locks, Exclusive locks, and Deadlock management.

What is Locking in SQL?

Locking in SQL refers to the process of preventing multiple transactions from modifying the same data at the same time, which can lead to inconsistent or incorrect results. Locks are applied to database objects (tables, rows, etc.) and can be shared (allowing read access by others) or exclusive (allowing write access by only one transaction). The purpose of locking is to maintain the ACID properties (Atomicity, Consistency, Isolation, and Durability) in a multi-user database environment.

1. Shared Lock

A Shared lock allows a transaction to read a resource (e.g., a row or a table) but not modify it. Multiple transactions can acquire Shared locks on the same resource simultaneously, as long as they are only reading the data. However, no transaction with a Shared lock can modify the data until all Shared locks are released.

Characteristics of Shared Lock:

Example:

        BEGIN TRANSACTION;

        -- Transaction 1: Acquire a shared lock on the row for reading
        SELECT * FROM Employees WHERE EmployeeID = 101;

        -- Transaction 2: Also acquire a shared lock to read the same row
        SELECT * FROM Employees WHERE EmployeeID = 101;

        COMMIT;
    

In this example, both Transaction 1 and Transaction 2 acquire a Shared lock on the row for Employee 101, allowing both to read the data simultaneously. Neither transaction can modify the data until the locks are released.

2. Exclusive Lock

An Exclusive lock allows a transaction to both read and modify a resource. When a transaction holds an Exclusive lock on a resource, no other transaction can access it for reading or writing until the lock is released. Exclusive locks are used when a transaction needs to ensure that it has sole access to a resource for updates or deletions.

Characteristics of Exclusive Lock:

Example:

        BEGIN TRANSACTION;

        -- Transaction 1: Acquire an exclusive lock to update the row
        UPDATE Employees SET Salary = 5500 WHERE EmployeeID = 101;

        -- Transaction 2: Try to acquire a shared lock (this will be blocked)
        SELECT * FROM Employees WHERE EmployeeID = 101;

        COMMIT;
    

In this example, Transaction 1 acquires an Exclusive lock on the row for Employee 101 in order to update the salary. Transaction 2 will be blocked from reading the row until Transaction 1 releases the lock, ensuring that the update is not interrupted by other transactions.

3. Deadlock

A Deadlock occurs when two or more transactions are waiting for each other to release locks, resulting in a cycle of dependencies that prevent any of the transactions from proceeding. In other words, each transaction is waiting for a resource that is locked by another transaction, and none of the transactions can proceed. Deadlocks can cause a significant performance issue, as they require intervention to resolve.

Characteristics of Deadlock:

Example:

        -- Transaction 1: Lock row A for update
        BEGIN TRANSACTION;
        UPDATE Employees SET Salary = 5000 WHERE EmployeeID = 101;

        -- Transaction 2: Lock row B for update
        BEGIN TRANSACTION;
        UPDATE Employees SET Salary = 6000 WHERE EmployeeID = 102;

        -- Transaction 1: Waits for row B, but row B is locked by Transaction 2
        UPDATE Employees SET Salary = 5500 WHERE EmployeeID = 102;

        -- Transaction 2: Waits for row A, but row A is locked by Transaction 1
        UPDATE Employees SET Salary = 6500 WHERE EmployeeID = 101;

        COMMIT;  -- This will result in a deadlock
    

In this example, Transaction 1 locks row A, and Transaction 2 locks row B. Each transaction attempts to update the row that is locked by the other, resulting in a deadlock. The database system will detect the deadlock and usually choose one of the transactions to roll back to resolve the issue.

Deadlock Detection and Resolution

Most modern database management systems (DBMS) have deadlock detection mechanisms in place. When a deadlock occurs, the DBMS will automatically identify the cycle of dependencies and resolve the deadlock by rolling back one of the transactions. The rolled-back transaction will typically receive an error message, and the application can handle the error by retrying the transaction.

For example, if Transaction 1 is rolled back, it will receive a deadlock error message, and it can be retried. The system ensures that no data is lost, and the transactions can proceed without any conflict.

Locking Mechanisms and Performance

Locking mechanisms are essential to maintaining data consistency and preventing conflicts in concurrent transactions. However, excessive locking can lead to performance degradation. For example:

It is important to choose the right locking mechanisms and isolation levels based on the specific requirements of the application and the desired level of concurrency.

Conclusion

Locking mechanisms in SQL—such as Shared locks, Exclusive locks, and Deadlock handling—play a crucial role in maintaining data integrity and ensuring that transactions execute in a consistent and isolated manner. Understanding how these locking mechanisms work helps database administrators and developers design efficient, high-performance systems that can handle concurrent transactions while preventing conflicts like dirty reads, lost updates, and deadlocks. Proper lock management is essential for achieving the right balance between concurrency and data integrity in a multi-user database environment.



Advertisement

Advertisement

Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java