Transaction and ACID properties in DBMS

Sam
3 min readNov 24, 2020

Understanding Transaction-

Transaction is a unit of program execution that accesses and possibly updates various data items.

The data item in a simplified model contain a single data value and is identified by a single letter for Eg-A,B,C etc.

Transaction access data using Two operations:

Read(X)-which reads the current value of the data item.

Write(X)-Which is assumed to update the database immediately after an operation is performed but in real database system, the write operation does not necessarily result In immediate update of data on the disk.

ACID PROPERTIES

Consider a transaction T1 that transfers from Rs 50 account A to account B.

T1:read(A);

A:-A-50;

Write(A);

Read(B);

B:-B+50;

Write(B);

Here A and B are the data-items and some operations of addition and subtraction is being performed.

Let us now consider each of the ACID properties using this transaction T1.

A stands for atomicity

Atomicity-It states that “Either all operations are reflected properly in the database, or none are”. It can be remembered as all-or-none property. Suppose the values of accounts A and B just before the execution of transaction T1 are 100 and 200 and suppose a failure occurs in T1 after the operation Write(A) was performed but before write(B) operation which leads to unsuccessful execution and as per the steps Rs 50 got deducted from the value of A but the value of B which is 200,remains the same.

Before transaction T1 was performed the sum of A+B was 300 but after it the sum is 250 as the value of A that got updated after the write(A) operation on the disk was 50 and since write(B) operation could not be performed so its value remain unchanged i.e. 200.There is clearly a loss of Rs 50.

Because of this failure the system is in inconsistent state as it failed to show the true state of the world that a database is supposed to capture. That is the reason for the necessity of atomicity which ensures that all actions of the transaction are reflected(done and updated) on the database or none are.

C stands for consistency-

Consistency-It states that if the database is consistent before the execution of the transaction, the should database remain consistent after the execution of the transaction.

For E.g.- If the sum of A+B is 300 before the execution of transaction then it should remain 300 after the completion of transaction, No matter how much Read and write operations are performed.

I stands for isolation

Isolation-It ensures that concurrently executing transactions are isolated from one and another, so that each has the impression that no other transaction is executing concurrently with it. Which means that Even though multiple transaction may execute concurrently , the system ensures that for, every pair of transactions Ti and Tj, it appears to Ti that Tj finished execution before Ti and Vice-versa. So, each transaction is unaware of the other transaction that is executed concurrently in the system.

For example consider two transaction T1 and T2

T1:read(A);

A:-A-50;

Write(A);

Read(B);

B:-B+50;

Write(B);

T2:read(A);

A:-A+50;

Write(A);

Read(B);

B:-B-50;

Write(B);

Suppose the current values of the account A and B are 100 and 200 respectively and suppose that the execution of the two transactions are done one at a time, in which T1 is followed by T2.So it can be easily found out that the sum of the money (A+B) is preserved after the execution of both the transaction T1and T2.This is only because of the isolation property of transaction.

D stands for Durability

Durability-It ensures that after a transaction is completed successfully, the changes it has made to the database persist, even if there are system failures.

Assume that a failure has occurred in the computer system which may result in the loss of data in the main memory but the data written on the disk are never lost. So durability is ensured by either of the following ways stated below.

1.The updates carried out by the transaction have been written on the disk before the transaction completes.

2.Information about the updates carried out by the transaction and written to disk is sufficient to enable the database to reconstruct the updates when the database system is restarted after the failure.

Reference-Database system concept by S.Sudarshan.

Happy Learning!!

--

--

Sam

“A engineer through education and a writer through random bursts of inspiration”