0
0
MySQLquery~10 mins

Lock types (shared, exclusive) in MySQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Lock types (shared, exclusive)
Transaction requests lock
Check lock type requested
Shared lock?
Check existing
locks compatibility
Grant shared lock
Transaction proceeds
Transaction releases lock
A transaction requests a lock type (shared or exclusive). The system checks compatibility with existing locks and grants the lock if allowed, then the transaction proceeds and later releases the lock.
Execution Sample
MySQL
BEGIN;
SELECT * FROM accounts WHERE id=1 LOCK IN SHARE MODE;
UPDATE accounts SET balance=balance+100 WHERE id=1;
COMMIT;
A transaction starts, acquires a shared lock for reading, then updates by upgrading to an exclusive lock, and commits.
Execution Table
StepActionLock RequestedExisting LocksLock Granted?Reason/Result
1BEGIN transactionNoneNoneNoneTransaction started, no locks yet
2SELECT with LOCK IN SHARE MODESharedNoneYesNo conflicting locks, shared lock granted
3UPDATE rowExclusiveShared (self)YesLock upgraded to exclusive (same transaction)
4UPDATE row (after acquiring exclusive lock)ExclusiveExclusive (self)YesExclusive lock already held, update allowed
5COMMIT transactionRelease allExclusiveReleasedLocks released, transaction ends
💡 Transaction ends after commit, all locks released
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
Lock HeldNoneSharedExclusiveExclusiveNone
Transaction StateNot startedActiveActiveActiveCommitted
Key Moments - 2 Insights
Why can the exclusive lock be granted immediately after holding a shared lock?
Because the transaction already holds a shared lock on the row (see step 3 in execution_table), MySQL allows automatic upgrade to exclusive lock for the same transaction.
What happens if another transaction holds an exclusive lock when a shared lock is requested?
The shared lock request will be blocked until the exclusive lock is released, because exclusive locks are not compatible with shared locks (see step 2 logic in concept_flow).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what lock is held after step 2?
AShared lock
BExclusive lock
CNo lock
DBoth shared and exclusive locks
💡 Hint
Check the 'Lock Granted?' and 'Lock Held' columns for step 2 in execution_table and variable_tracker
At which step does the transaction finally get the exclusive lock?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look at the 'Lock Granted?' column in execution_table for when exclusive lock is granted
If the transaction did not release the shared lock before requesting exclusive lock, what would happen?
AExclusive lock request is denied or blocked
BShared lock is upgraded automatically
CExclusive lock granted immediately
DTransaction deadlocks
💡 Hint
Refer to step 3 in execution_table and key_moments about lock upgrade behavior
Concept Snapshot
Lock types in MySQL:
- Shared lock (S): allows multiple transactions to read but not write.
- Exclusive lock (X): allows one transaction to write, blocks others.
- Shared and exclusive locks are incompatible.
- Transactions must release or upgrade locks properly.
- Locks are held until transaction commits or rolls back.
Full Transcript
This visual execution shows how MySQL handles shared and exclusive locks during a transaction. The transaction begins without locks, then requests a shared lock for reading. The shared lock is granted because no conflicting locks exist. When the transaction tries to update the row, it needs an exclusive lock. MySQL automatically upgrades the shared lock to exclusive because it's the same transaction. The update succeeds. Finally, the transaction commits and releases all locks. This demonstrates lock compatibility and upgrade behavior within a transaction.