A Database Transaction is a logical unit of processing in a DBMS which entails one or more database access operation. It is a sequence of operations that form a single unit of work.
A transaction is often initiated by an application program
- Begin a transaction
- Ends a transaction
COMMIT (if successful) or ROLLBACK (if errors)
A transaction T transforms one consistent database state into another consistent database state. During the execution of T, the database may be temporarily inconsistent.
Either the whole transaction must be succeed or the effect of all operations has to be undone (rollback).
There are two main transaction issues
- concurrent execution of multiple transactions
- recovery after hardware failures and system crashes
DBMS is the management of data that should remain integrated when any changes are done in it. It is because if the integrity of data is affected, whole data will get distributed and corrupt. Therefore, to maintain integrity of the data, there are four properties which are known as ACID properties.
Operations in Transaction
Following are the two main operations in a transaction:
- Read operation
- Write operation
Read Operation:This operation transfers the data item from the database and then stores it in a buffer in main memory.
Write Operation:This operation writes the updated data value back to the database from the buffer.
For example: Let T be a transaction that transfer Rs50 from X account to Y account. This transaction can be defined as:
T : read(X); X := X – 50; write(X); read(Y); Y := Y + 50; write(Y);
Assume that the value of both X and Y before starting of the transaction T is 100.
- The first operation read the value of X (100) from the database and stores it in a buffer. The second operation will decrease X value by 50. So buffer will contain 50. The third operation will write the value of the buffer to the database. So the final value of X will be 50.
- The fourth operation read the value of Y (100) from the database and stores it in a buffer. The fifth operation will add X value by 50. So buffer will contain 150. The sixth operation will write the value of buffer to the database. So the final value of Y will be 150.
ACID properties are used for maintaining the integrity of database during transaction processing. ACID stands for Atomicity, Consistency, Isolation, and Durability.
A transaction is a single unit of operation. You either execute it entirely or do not execute it at all.
For example, in an application that transfers funds from one account to another, the atomicity property ensures that, if a debit is made successfully from one account, the corresponding credit is made to the other account.
Once the transaction is executed, it should move from one consistent state to another.
For example, in an application that transfers funds from one account to another, the consistency property ensures that the total value of funds in both the accounts is the same at the start and end of each transaction.
Even though transactions are executing concurrently, they should appear to be executed in isolation – that is, their final effect should be as if each transaction was executed in isolation from start to finish.
For example, in an application that transfers funds from one account to another, the isolation property ensures that another transaction sees the transferred funds in one account or the other, but not in both, nor in neither.
Once a transaction is committed, its changes (writes) applied to the database must never be lost because of subsequent failure.
For example, in an application that transfers funds from one account to another, the durability property ensures that the changes made to each account will not be reversed.