Transactions in DBMS

In the previous blog, we learned about the basics of database. Today let’s explore this a bit more into the topic. So our topic of discussion today will be transactions.

So…what are transactions??

Simply put, a transaction is a logical unit of processing in the Database Management System.
In order to successfully execute, a transaction must go through various steps from the below flow diagram.

Transaction Flow Diagram
Transaction Flow Diagram

Let’s learn about each of the stages in the transaction flow diagram:

Active State

  1. When the transaction (query) is executed by the user, the database will make the transaction active and check for any syntax or semantic errors.
  2. CPU will load the table into temporary storage (RAM).
  3. If there are NO ERRORS, then proceed to the partially committed state else proceed to failed state.

Partially Committed State

  1. The table that was loaded into the temporary storage is updated at this stage as per the transaction executed.
  2. If the updation happened properly, then proceed to the committed state else proceed to failed state.

Committed State

  1. Once the changes are properly done in the temporary storage (RAM) the same changes need to be done in the secondary storage.
  2. Once the changes are made successfully in the secondary storage, proceed to the terminated state.

Terminated State

Whether the transaction is committed or aborted, free the temporary memory for future transactions.

Failed State

Proceed to the aborted state.

Aborted State

Kill the transaction and proceed to the terminated state.

ACID Properties

ACID properties are the four properties that are required to maintain the integrity of the database.

But…why do we need to know about ACID properties??

Let us understand this by an example.
Case Scenario: Suppose you are A and you want to send $50 to your friend B. You sent the money via online transfer.
The amount was deducted from your account but B didn’t receive it, and moreover, you don’t even get a refund. So where did the money go??
It is the ACID properties that kind of save you from such uncertainties!!!
Let’s see how.

ACID Properties
ACID Properties

Atomicity

Atomicity states that a transaction should either take place in full or not at all.
Let’s go back to our case scenario. During the money transfer from A to B either money should be transferred successfully or it has to be rolled back to the account from which it was initially transferred.

Consistency

Consistency states that the values are consistent before and after the transactions take place.
Let’s go back to our case scenario. If transferred $50 to B, then $50 must be debited from account A and the same should be credited to account B.

Isolation

Isolation states that if multiple transactions are occurring at the same time, they will occur independently, that is, one transaction must not interfere with another transaction in progress.\

Durability

Durability states that the changes made to the database should not be lost even in the case of a system crash. The data should be available in secondary storage until the next change occurs.

Wrapping it up…

In this blog, we have discussed the basics of Databases.
In the coming blogs, I’ll be discussing more advanced concepts of Databases and how to connect Databases to Java applications using JDBC and Hibernate.
If Databases interest you and you want to pursue a professional certification for Databases you can refer to this course by Udacity.

Related Topics: