> For the complete documentation index, see [llms.txt](https://huy312100.gitbook.io/software-development/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://huy312100.gitbook.io/software-development/dbms/fundamental/transaction-concurrency-control-techniques/isolation-level.md).

# Isolation level

The SQL standard defines four levels of transaction isolation, each providing a different level of protection against potential issues caused by concurrent transactions:

1. **Read Uncommitted**
   * **Behavior**: Transactions can see uncommitted changes made by other transactions.
   * **Potential Issues**:
     * **Dirty Read**: Reading data written by a transaction that hasn’t been committed yet.
   * **Example**:

     ```sql
     SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
     BEGIN TRANSACTION;
     SELECT * FROM Orders WHERE OrderID = 1;
     COMMIT;
     ```
2. **Read Committed**
   * **Behavior**: Transactions can only see changes made by other transactions once they are committed.
   * **Potential Issues**:
     * **Non-Repeatable Read**: Data read once within a transaction can change if read again.
   * **Example**:

     ```sql
     SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
     BEGIN TRANSACTION;
     SELECT * FROM Orders WHERE OrderID = 1;
     COMMIT;
     ```
3. **Repeatable Read**
   * **Behavior**: Ensures that if a transaction reads a row, it will read the same data if it reads that row again during the same transaction.
   * **Potential Issues**:
     * **Phantom Read**: New rows can be added by other transactions and will be seen by subsequent reads.
   * **Example**:

     ```sql
     SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;
     BEGIN TRANSACTION;
     SELECT * FROM Orders WHERE CustomerID = 1;
     -- Do some work
     SELECT * FROM Orders WHERE CustomerID = 1; -- Same result as the first select
     COMMIT;
     ```
4. **Serializable**
   * **Behavior**: The highest isolation level, ensuring that transactions are completely isolated from each other, effectively serializing them.
   * **Potential Issues**: No issues but can significantly impact performance.
   * **Example**:

     ```sql
     SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
     BEGIN TRANSACTION;
     SELECT * FROM Orders WHERE OrderDate = '2023-01-01';
     -- Do some work
     SELECT * FROM Orders WHERE OrderDate = '2023-01-01'; -- Same result as the first select
     COMMIT;
     ```

#### Practical Transaction Management

Here’s a more detailed example including savepoints and handling exceptions:

```sql
BEGIN TRANSACTION;

-- Try to perform multiple operations
BEGIN TRY
    -- Insert a new customer
    INSERT INTO Customers (CustomerID, Name) VALUES (1, 'John Doe');
    SAVEPOINT InsertCustomer;

    -- Update an existing customer's address
    UPDATE Customers SET Address = '123 Main St' WHERE CustomerID = 1;
    SAVEPOINT UpdateAddress;

    -- Perform another operation, e.g., delete an order
    DELETE FROM Orders WHERE OrderID = 1;

    -- If all operations succeed, commit the transaction
    COMMIT;
END TRY
BEGIN CATCH
    -- If any operation fails, rollback to the last savepoint
    ROLLBACK TO SAVEPOINT UpdateAddress;

    -- Or rollback the entire transaction
    ROLLBACK;

    -- Optionally, handle the error, e.g., log it
    PRINT ERROR_MESSAGE();
END CATCH;
```

#### Summary

Managing transactions at a detailed level in databases involves understanding and implementing the ACID properties, choosing appropriate isolation levels, and handling transactions effectively to ensure data integrity and consistency. This detailed approach is crucial in systems that require high reliability and robustness, such as financial systems, e-commerce platforms, and critical business applications.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://huy312100.gitbook.io/software-development/dbms/fundamental/transaction-concurrency-control-techniques/isolation-level.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
