Introduction to Database Transaction
In our real life, we deposit money from one account to another account. Ever wondered, how this happens in the database?
Suppose, Alex wants to send money to Bob. There are three things we as a developer need to take care of.
What if after completing the second operation and before completing the third operation, there is a problem in the database and it couldn't execute the third operation. So, the money isn't deposited to Bob's account but deducted from Alex's account. Alex tells Bob, "Hey, I have sent the money to your account." but Bob replies, "I haven't received the money".
As a developer, how will we fix this inconsistent behavior? How do we ensure consistency in data in the database?
To fix this problem, Database has a beautiful concept called Transaction.
What is Database Transaction?
A transaction is a collection of queries that are treated as one unit of work. One unit of work means, either every individual query will be successful or nothing will happen.
Before jumping to writing queries we need to know 3 important topics about transactions.
The structure of a transaction looks like below:
To solve the previous problem let's consider there is a table called Accounts. There we have two columns named AccountNumber and Balance. Now the query will look like below:
Recommended by LinkedIn
DECLARE @senderAccountNumber INT = 123;
DECLARE @receiverAccountNumber INT = 456;
DECLARE @transferAmount DECIMAL(18, 2) = 100.00;
BEGIN TRANSACTION;
BEGIN TRY
IF EXISTS (
SELECT 1
FROM Accounts
WHERE AccountNumber = @senderAccountNumber
AND Balance >= @transferAmount
)
BEGIN
UPDATE Accounts
SET Balance = Balance - @transferAmount
WHERE AccountNumber = @senderAccountNumber;
UPDATE Accounts
SET Balance = Balance + @transferAmount
WHERE AccountNumber = @receiverAccountNumber;
END
COMMIT TRANSACTION;
END TRY
BEGIN CATCH
ROLLBACK;
END CATCH
For example, we assumes that Alex is sending 100$ to Bob. Alex and Bob's account number is 123 and 456 respectively. But in real life these three information will be dynamic.
Code Explanation:
In IF EXISTS checks whether sender has enough money or not. If sender has enough money in his/her account then we are deducting the money from sender's account and deposit it to receiver's account.
In this process if there occurs any problem then the whole transaction will rollback and the amount in sender's and receiver's account will be same as before.
Pros:
Cons:
Best Practices:
Database transaction is a very useful concept for maintaining reliability, consistency and integrity. By following best practices, transactions can significantly improve the performance.
That’s all for today. Happy Databasing 😁
The transaction feature really helps out in situations where there are lots of changes happening at once. I use it often when I need to handle multiple write/edit operations and need to maintain data integrity.
Simple and easy to understand. Thanks for the insights vaia.
Just learned and implemented transaction two days ago. Found this article insightful. Keep writing vai.
Before committing, does the engine write the data to the disk or to the memory?