Class 10 | Database Transaction | DBMS Notes

Transaction

A Transaction is a set of operations performed as a logical unit of work.

A transaction generally represents a change in a database.

It is the result of a request made by a user to access the contents of a database and perform operations on it.

A transaction:

  • Consists of different operations
  • Passes through different states
  • Follows specific properties
  • Helps maintain database consistency

Operations of Transaction

A user can perform different types of operations to access or modify the contents of a database.

The main transaction operations are:

  1. Read(X)
  2. Write(X)
  3. Commit
  4. Rollback

1. Read(X)

A Read(X) operation is used to read the value of X from the database and store it temporarily in a buffer in the main memory.

The value can then be used for further actions such as displaying it to the user.

A read operation is generally performed when the user only wants to view data without making any changes.

Example:

When a user wants to check the balance of a bank account, a read operation is performed to retrieve the account balance from the database.

2. Write(X)

A Write(X) operation is used to write a value from the main-memory buffer back into the database.

Before performing a write operation, a read operation is generally performed first.

The process may include:

  1. Reading the existing value from the database.
  2. Storing it in a buffer.
  3. Performing some operation on the value.
  4. Writing the modified value back to the database.

Example:

Suppose a user wants to withdraw money from a bank account.

First, the account balance is retrieved using a read operation.

Then:

  • The withdrawal amount is subtracted.
  • The new balance is calculated.
  • The modified balance is written back to the database using a write operation.

3. Commit

A Commit operation is used to permanently save the changes made by a transaction into the database.

A transaction may be interrupted because of:

  • Power failure
  • Hardware failure
  • Software failure
  • Other system errors

If a transaction is interrupted before all its operations are completed, the database may become inconsistent.

The commit operation ensures that changes are permanently stored only after the required transaction operations have been successfully completed.

4. Rollback

A Rollback operation is used to return the database to its last saved consistent state.

It is generally performed when a transaction is interrupted because of:

  • Power failure
  • Hardware failure
  • Software failure
  • Other errors

In simple terms, rollback undoes the operations performed before the transaction was interrupted.

This helps avoid:

  • Data inconsistency
  • Ambiguity
  • Partial transaction results

States of Transaction

A transaction passes through different states during its execution.

The main transaction states are:

  1. Active
  2. Partially Committed
  3. Committed
  4. Failed
  5. Aborted

1. Active State

The Active State is the first stage of a transaction.

In this state, the transaction has started executing.

Operations such as the following may be performed:

  • Insertion
  • Deletion
  • Updation

During this state, data records are being manipulated.

The changes are not yet permanently saved in the database and may remain temporarily in a buffer in the main memory.

2. Partially Committed State

A transaction enters the Partially Committed State when most of its operations have been completed and it is executing its final operation.

After the final operation is completed, the transaction is expected to perform the commit operation.

If an error occurs during this stage, the transaction enters the Failed State.

If no error occurs, it enters the Committed State.

3. Committed State

A transaction enters the Committed State when:

  • All transaction operations have completed successfully.
  • The commit operation has been successfully performed.
  • The modified data has been permanently saved into the database.

This state represents the successful completion of a transaction.

4. Failed State

A transaction enters the Failed State when an error occurs during the:

  • Active State
  • Partially Committed State

When this happens, further execution of the transaction is stopped.

The database recovery system then attempts to ensure that the database remains in a consistent state.

5. Aborted State

If the error cannot be resolved in the Failed State, the transaction enters the Aborted State.

A rollback operation is then performed to restore the database to its last consistent state.

After a transaction is aborted, the database recovery module may:

  • Restart the transaction
  • Terminate the transaction

Figure: Add transaction state diagram here.

Properties of Transaction

Transactions access and modify database contents.

Therefore, they must follow certain properties to maintain database:

  • Consistency
  • Integrity
  • Reliability

Transactions follow four important properties:

  1. Atomicity
  2. Consistency
  3. Isolation
  4. Durability

These are commonly known as the ACID Properties of a transaction.

ACID Properties

ACID stands for:

  • A – Atomicity
  • C – Consistency
  • I – Isolation
  • D – Durability

1. Atomicity

Atomicity ensures that either all operations of a transaction are completed successfully or none of them are permanently applied.

A transaction cannot be partially completed.

Each transaction is treated as a single unit, similar to an atom.

Atomicity is maintained using:

  • Commit
  • Rollback

If all operations are completed successfully, the changes are committed to the database.

If the transaction is interrupted, the performed changes are rolled back and the database returns to its previous consistent state.

2. Consistency

Consistency ensures that the database remains in a valid and consistent state before and after a transaction.

The execution of a transaction should transform the database from:

One consistent state → Another consistent state

After the transaction is completed, the database should either:

  • Remain in its previous stable state, or
  • Move to a new stable state

The changes should be the result of valid logical operations requested by the user.

3. Isolation

Isolation ensures that two transactions do not improperly interfere with each other.

If one transaction is using particular data, another transaction should not interfere with that data in a way that creates inconsistent results.

This property helps maintain database integrity when multiple transactions are executed.

Transactions should behave as though they are isolated from one another.

According to the notes, this property is enforced by the Concurrency Control Subsystem of the DBMS.

4. Durability

Durability ensures that once a transaction has been successfully completed and committed, its changes remain permanent.

The committed changes should remain intact even if the system later experiences:

  • Failure
  • Crash
  • Power loss

The database should preserve the consistent state achieved after the successful completion of the transaction.

According to the notes, the Recovery Subsystem of DBMS is responsible for enforcing durability.

Concurrency Control

When multiple transactions take place at the same time, it is called Concurrency.

Concurrency Control is an important concept in DBMS that manages the simultaneous execution or manipulation of data by multiple users or processes.

Its main purpose is to allow multiple transactions to operate without causing:

  • Data inconsistency
  • Incorrect results
  • Conflicts between transactions

Advantages of Concurrency Control

The advantages of concurrency control include:

  1. Waiting time is decreased.
  2. Response time is reduced.
  3. Resource utilization is increased.
  4. System performance is improved.
  5. System efficiency is increased.

Discussion

Share a helpful question, idea, or explanation with other students.

Leave a Comment

Write a clear question, answer, or helpful explanation.
Your email will not be published.

Download Our Offline App

Study class-wise notes even when internet is not available. Get the app from Play Store.

Nepal eNotes offline app preview
Get it on Google Play