Chapter 17: Transactions & Concurrency

Transactions are what make MySQL (and any good database) trustworthy — especially when multiple people or programs are using the database at the same time.

Today we’ll learn:

  • What ACID really means (why your money doesn’t disappear!)
  • How to use START TRANSACTION, COMMIT, ROLLBACK
  • SAVEPOINT — like mini checkpoints
  • Isolation levels — how much one user can see what others are doing
  • LOCK TABLES — when you really need to stop everyone else

Let’s use a real-life bank example to make everything crystal clear.

Imagine two bank accounts:

  • Account A (Priya) — balance = ₹10,000
  • Account B (Rahul) — balance = ₹5,000

Priya wants to transfer ₹3,000 to Rahul.

This should happen in one atomic operation:

  1. Subtract ₹3,000 from Priya
  2. Add ₹3,000 to Rahul

If anything fails in the middle (power cut, crash, error), neither account should change — otherwise money would vanish or appear from nowhere!

1. ACID Properties – The 4 Golden Rules of Transactions

Letter Property What it means Bank Example
A Atomicity All or nothing — entire transaction succeeds or fails completely Either both accounts update, or neither does
C Consistency Database remains in a valid state before & after transaction Total money in bank stays ₹15,000 before & after
I Isolation Transactions don’t interfere with each other until committed Another user can’t see Priya’s balance mid-transfer
D Durability Once committed, changes are permanent — even if server crashes After commit, ₹3,000 is safely transferred forever

MySQL’s InnoDB storage engine (default) fully supports ACID!

2. START TRANSACTION, COMMIT, ROLLBACK

Syntax:

SQL

Example – Safe bank transfer

First, create sample tables:

SQL

Now the safe transfer:

SQL

If something goes wrong (simulate error):

SQL

Real power: If the server crashes after COMMIT → changes are saved (Durability). If crash before COMMIT → changes are automatically rolled back (Atomicity).

3. SAVEPOINT – Mini Checkpoints Inside a Transaction

Syntax:

SQL

Example – Transfer with safety checkpoints

SQL

Result: Priya’s account is deducted, Rahul’s is not — we can fix and retry the add.

4. Isolation Levels – How Much One Transaction Sees Others

MySQL has 4 isolation levels (from strictest to loosest):

Level What it prevents Dirty Read? Non-repeatable Read? Phantom Read? Performance
READ UNCOMMITTED Almost nothing Yes Yes Yes Fastest
READ COMMITTED Dirty reads No Yes Yes Good
REPEATABLE READ Dirty + Non-repeatable reads (MySQL default) No No Yes Balanced
SERIALIZABLE All anomalies No No No Slowest

Real-life example of problems:

Dirty Read (READ UNCOMMITTED): Priya sees Rahul’s balance as ₹8,000 mid-transfer — but if Rahul rolls back, she saw fake data!

Non-repeatable Read (READ COMMITTED): Priya reads her balance twice in one transaction → second time it’s changed by Rahul’s transfer!

Phantom Read (REPEATABLE READ): Priya counts customers with balance > ₹5000 → first time 10, second time 11 because Rahul added a new customer.

How to set isolation level:

SQL

Check current level:

SQL

5. LOCK TABLES – When You Need Exclusive Access

Syntax:

SQL

READ lock: Others can read, but no one can write WRITE lock: Only you can read/write — others wait

Example – Safe bulk update (prevent others from interfering)

SQL

Caution: LOCK TABLES can cause deadlocks or slow performance — use only when necessary. InnoDB usually handles row-level locking automatically — prefer SELECT … FOR UPDATE for row locks.

Modern way (row-level lock):

SQL

That’s it for Chapter 17! 🎉 You now understand how to keep data safe, consistent, and isolated even when 1000 users are hitting your database at once!

Homework for today (do it right now – very important!) Run these commands and paste the output of the final SELECTs:

SQL

Paste the results of the three SELECT * FROM accounts; — I’ll check if transactions worked correctly!

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *