DEV Community

Rithika
Rithika

Posted on

DBMS: TRANSACTIONS,DEADLOCKS & LOG- BASED RECOVERY

🔐 Mastering MySQL Transactions and Row-Level Locking: A Practical Walkthrough

When working with relational databases, one of the biggest challenges developers face is maintaining data consistency when multiple users or applications try to access and modify data at the same time.

Imagine several banking transactions happening simultaneously — one user transferring money, another updating their profile, and a third checking their balance. Without proper control, these operations could interfere with each other, leading to inaccurate or inconsistent data.

🧩 Scenario 1: Locking Rows with SELECT ... FOR UPDATE

To start, let’s consider a simple example that demonstrates row-level locking — an essential feature that prevents multiple users from changing the same record at the same time.

START TRANSACTION;

Lock Riya's row
SELECT * FROM Accounts WHERE name = 'Riya' FOR UPDATE;
Update Arjun's balance (allowed if not locked by another session)
UPDATE Accounts SET balance = balance + 10 WHERE name = 'Arjun';

In the above example, we begin a transaction using the START TRANSACTION command. The statement SELECT ... FOR UPDATE is used to lock Riya’s row, which means that as long as this transaction remains active, no other user or session can modify that particular record. This kind of locking is incredibly useful in scenarios like banking systems or online wallets, where two users might attempt to modify the same account balance simultaneously.

By locking the row until the transaction is either committed or rolled back, MySQL ensures that data changes are applied safely and in the correct order. Meanwhile, other rows — such as Arjun’s — remain accessible for updates by other transactions, allowing parallel processing without conflicts.

💾 Scenario 2: Rolling Back a Transaction

Now, let’s look at how MySQL lets you undo changes using the ROLLBACK command. This feature is crucial whenever an operation fails or you need to revert modifications after an error.

START TRANSACTION;
Transfer funds from Riya to Arjun
UPDATE Accounts SET balance = balance - 500 WHERE name = 'Riya';
UPDATE Accounts SET balance = balance + 500 WHERE name = 'Arjun';

ROLLBACK;

Verify balances
SELECT * FROM Accounts;

Here, the idea is simple: Riya is transferring ₹500 to Arjun. Both updates happen inside a transaction, meaning MySQL treats them as a single logical unit of work. If everything goes as expected, the transaction can be committed, permanently saving the changes. But if something goes wrong midway — for example, a constraint fails or the server crashes — you can issue a ROLLBACK command to revert all changes made in that session.

This approach guarantees that your data never ends up in an incomplete or inconsistent state. It’s like having an undo button for your database operations — one that ensures reliability and prevents data corruption.

🔄 Scenario 3: Locking Arjun, Updating Riya

In the final example, let’s flip the situation around. Here, we’ll lock one user’s record and update another’s, illustrating how MySQL provides fine-grained concurrency control.

START TRANSACTION;

Lock Arjun's row
SELECT * FROM Accounts WHERE name = 'Arjun' FOR UPDATE;
Update Riya's balance
UPDATE Accounts SET balance = balance + 20 WHERE name = 'Riya';

💡 Key Takeaways

Transactions ensure that multiple operations are treated as one unit — either all succeed or all fail.

Row-level locking prevents data conflicts by restricting access to specific rows during transactions.

ROLLBACK allows you to revert changes safely in case of errors or failed operations.

These mechanisms together help maintain data consistency, accuracy, and system stability, even under heavy load.

🚀 Final Thoughts

Exploring these MySQL features through Oracle Live SQL or your local environment can really strengthen your understanding of how databases maintain order in complex, multi-user systems. Once you start experimenting with transactions and locks, you’ll realize how essential they are for real-world applications like banking, e-commerce, and analytics.

The more you play with these commands — testing commits, rollbacks, and concurrent updates the more confident you’ll become in building safe, reliable, and high-performance database systems.

Special thanks to @santhoshnc sir for guidance throughout this assignment.



Top comments (0)