# Transactions & Concurrency

The transaction is a **single logical unit that accesses and modifies the contents of the database**. Transactions access data using **read and write** operations. Transaction is a single operation of processing that can have many operations. Transaction is needed when more than one user wants to access same database. Transaction has ACID properties.

&#x20;

<figure><img src="https://media.geeksforgeeks.org/wp-content/uploads/20201101105449/Transaction_state.png" alt=""><figcaption><p>Credits GFG</p></figcaption></figure>

### ACID Properties of Transaction:

ACID stands for Atomicity, Consistency, Isolation and Durability

1. **Atomicity:** All the operations in a transaction are considered to be atomic and as one unit. If system fails or any read/write conflicts occur during transaction the system needs to revert back to its previous state. Atomicity is maintained by the Transaction Management Component.
2. **Consistency:** Every transaction should lead to database connection from one valid state to other valid state. If system fails because of invalid data while doing an operation revert back the system to its previous state. Consistency is maintained by the Application manager.
3. **Isolation:** If multiple transactions are executing on single database, each transaction should be isolated from other transaction. If multiple transactions are performed on single database, operation from any transaction should not interfere with operation in other transaction. Isolation is maintained by the concurrency control manager.&#x20;
4. **Durability:** Durability means the changes made during the transactions should exist after completion of transaction. Changes must be permanent and must not be lost due to any database failure. It is maintained by the recovery manager.

<figure><img src="https://media.geeksforgeeks.org/wp-content/cdn-uploads/20191121102921/ACID-Properties.jpg" alt=""><figcaption></figcaption></figure>

### Example:

A has an account with an amount of Rs 150. B has an account with an amount of Rs 50. A is transferring amount Rs 100 to B’s account.

1. **Atomicity:** Operations required for transfer are: Deduct amount Rs100 from A’s account. Add amount Rs 100 to B’s account. All operations should be done. If system fails to add amount in B’s account after deducting from A’s account, revert the operation on A’s account.
2. **Consistency:** The sum amount in A’s account and B’s account should be same before and after the transaction completes. In the example the sum of both account before and after transaction is Rs 200, which preserves the consistency.
3. **Isolation:** If there is any other transaction (let between A and C) is going on, it should not affect the transaction between A and B i.e., both the transactions should be isolated.
4. **Durability:** It may happen system gets crashed after the completion of all operations then, after restarting it should preserve all the changes. The amount in A’s and B’s account should be same before and after the system restart.


---

# Agent Instructions: 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:

```
GET https://codexpress.gitbook.io/welcome-to-datagenesis/concurrency-control/transactions-and-concurrency.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
