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

Isolation Levels (Read Uncommitted, Read Committed, Repeatable Read, Serializable) in SQL


In SQL, isolation levels are used to control the visibility of changes made by one transaction to other concurrent transactions. These isolation levels determine how transactions interact with each other, particularly in terms of visibility of data being modified. The SQL standard defines four isolation levels: Read Uncommitted, Read Committed, Repeatable Read, and Serializable. Each level offers a trade-off between concurrency and consistency.

What is Transaction Isolation?

Transaction isolation refers to the degree to which the operations in one transaction are isolated from the operations in other concurrent transactions. The higher the isolation level, the more isolated a transaction is from others. However, higher isolation levels may reduce the system’s concurrency, leading to performance issues in highly concurrent environments. The four isolation levels are designed to control the level of interaction between transactions in terms of reading and writing data.

1. Read Uncommitted

The Read Uncommitted isolation level is the lowest level of isolation. In this level, a transaction can read data that has been modified but not yet committed by other transactions. This is known as a "dirty read." Because of this, data might be inconsistent or incorrect, as the uncommitted changes from other transactions could be rolled back later.

Characteristics of Read Uncommitted:

Example:

        SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;

        BEGIN TRANSACTION;

        -- Transaction 1: Update the Salary of an employee
        UPDATE Employees SET Salary = 6000 WHERE EmployeeID = 101;

        -- Transaction 2: Read the updated Salary (can read uncommitted data from Transaction 1)
        SELECT Salary FROM Employees WHERE EmployeeID = 101;

        COMMIT;
    

In this example, Transaction 2 can read the updated salary from Transaction 1 even if Transaction 1 has not yet committed the changes, leading to the possibility of a dirty read.

2. Read Committed

The Read Committed isolation level ensures that a transaction can only read data that has been committed by other transactions. This prevents dirty reads, but it does not prevent non-repeatable reads. A non-repeatable read occurs when a value that was read by one transaction is modified by another transaction before the first transaction is completed.

Characteristics of Read Committed:

Example:

        SET TRANSACTION ISOLATION LEVEL READ COMMITTED;

        BEGIN TRANSACTION;

        -- Transaction 1: Read the Salary of an employee
        SELECT Salary FROM Employees WHERE EmployeeID = 101;

        -- Transaction 2: Update the Salary of the same employee
        UPDATE Employees SET Salary = 6500 WHERE EmployeeID = 101;

        -- Transaction 1: Re-read the Salary (it will see the updated value after Transaction 2 commits)
        SELECT Salary FROM Employees WHERE EmployeeID = 101;

        COMMIT;
    

In this example, Transaction 1 reads the salary of Employee 101. Transaction 2 updates the salary, and when Transaction 1 re-reads the salary, it sees the updated value. This is an example of a non-repeatable read.

3. Repeatable Read

The Repeatable Read isolation level prevents both dirty reads and non-repeatable reads. This level ensures that once a transaction reads a value, no other transaction can modify that value until the transaction completes. However, it does not prevent phantom reads. A phantom read occurs when a transaction reads a set of rows based on a condition, but another transaction inserts or deletes rows that meet the same condition before the first transaction is finished.

Characteristics of Repeatable Read:

Example:

        SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;

        BEGIN TRANSACTION;

        -- Transaction 1: Read the Salary of an employee
        SELECT Salary FROM Employees WHERE EmployeeID = 101;

        -- Transaction 2: Update the Salary of the same employee (Transaction 1 cannot see the change yet)
        UPDATE Employees SET Salary = 7000 WHERE EmployeeID = 101;

        -- Transaction 1: Re-read the Salary (it will see the same value as initially read)
        SELECT Salary FROM Employees WHERE EmployeeID = 101;

        COMMIT;
    

In this example, Transaction 1 will read the same salary value twice, even though Transaction 2 attempts to update it. The salary value will remain unchanged for Transaction 1 due to the Repeatable Read isolation level.

4. Serializable

The Serializable isolation level provides the highest level of isolation. It ensures that transactions are executed in such a way that the results are equivalent to running the transactions serially, one after the other, rather than concurrently. This level prevents dirty reads, non-repeatable reads, and phantom reads. However, it has the highest potential for blocking and can result in reduced concurrency.

Characteristics of Serializable:

Example:

        SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;

        BEGIN TRANSACTION;

        -- Transaction 1: Read the Salary of an employee
        SELECT Salary FROM Employees WHERE EmployeeID = 101;

        -- Transaction 2: Attempt to insert a new employee (Transaction 1 is locked until commit)
        INSERT INTO Employees (EmployeeID, Name, Position, Salary)
        VALUES (104, 'David Miller', 'HR', 5000);

        -- Transaction 1: Re-read the Salary (it will see the same value as initially read)
        SELECT Salary FROM Employees WHERE EmployeeID = 101;

        COMMIT;
    

In this example, Transaction 1 will lock the record of Employee 101, preventing Transaction 2 from inserting or modifying any records that affect the outcome of Transaction 1. This isolation level ensures serial execution, but it might cause performance issues in high-concurrency environments.

Comparison of Isolation Levels

Isolation Level Dirty Reads Non-Repeatable Reads Phantom Reads Concurrency
Read Uncommitted Allowed Allowed Allowed Highest
Read Committed Not Allowed Allowed Allowed High
Repeatable Read Not Allowed Not Allowed Allowed Medium
Serializable Not Allowed Not Allowed Not Allowed Lowest

Conclusion

In SQL, the choice of isolation level has a significant impact on the behavior of transactions and the database system's performance. The four isolation levels—Read Uncommitted, Read Committed, Repeatable Read, and Serializable—balance the trade-off between data consistency and concurrency. Understanding these isolation levels and choosing the right one for your application can help ensure both the accuracy and performance of your database transactions.



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