Bird
Raised Fist0
MySQLquery~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of a shared lock in MySQL?
easy
A. Allow multiple users to read data simultaneously without conflicts
B. Prevent any user from reading data
C. Allow only one user to write data at a time
D. Delete data safely from the database

Solution

  1. Step 1: Understand shared lock behavior

    A shared lock allows many users to read the same data at the same time safely.
  2. Step 2: Compare with other lock types

    Exclusive locks prevent others from reading or writing, but shared locks only prevent writing.
  3. Final Answer:

    Allow multiple users to read data simultaneously without conflicts -> Option A
  4. Quick Check:

    Shared lock = multiple readers allowed [OK]
Hint: Shared locks allow many readers, exclusive locks block others [OK]
Common Mistakes:
  • Confusing shared lock with exclusive lock
  • Thinking shared locks block reading
  • Assuming shared locks allow writing
2. Which of the following is the correct syntax to acquire a shared lock on a table in MySQL?
easy
A. LOCK TABLES table_name WRITE;
B. LOCK TABLES table_name SHARE;
C. LOCK TABLES table_name EXCLUSIVE;
D. LOCK TABLES table_name READ;

Solution

  1. Step 1: Recall MySQL lock syntax

    In MySQL, to acquire a shared lock, you use LOCK TABLES table_name READ;.
  2. Step 2: Eliminate incorrect options

    WRITE is for exclusive locks, EXCLUSIVE and SHARE are not valid MySQL lock keywords.
  3. Final Answer:

    LOCK TABLES table_name READ; -> Option D
  4. Quick Check:

    Shared lock syntax uses READ keyword [OK]
Hint: Use READ for shared lock, WRITE for exclusive lock [OK]
Common Mistakes:
  • Using WRITE instead of READ for shared lock
  • Using unsupported keywords like EXCLUSIVE or SHARE
  • Confusing lock syntax with SQL SELECT statements
3. Consider two transactions in MySQL: Transaction A acquires a shared lock on a row, and Transaction B tries to acquire an exclusive lock on the same row. What happens?
medium
A. Transaction B immediately acquires the exclusive lock
B. Transaction B waits until Transaction A releases the shared lock
C. Transaction A releases its shared lock automatically
D. Both transactions acquire locks simultaneously

Solution

  1. Step 1: Understand lock compatibility

    Shared locks allow multiple readers but block exclusive locks until released.
  2. Step 2: Analyze the scenario

    Transaction B must wait for Transaction A to release the shared lock before getting exclusive lock.
  3. Final Answer:

    Transaction B waits until Transaction A releases the shared lock -> Option B
  4. Quick Check:

    Exclusive lock waits for shared locks to release [OK]
Hint: Exclusive locks wait if shared locks exist on data [OK]
Common Mistakes:
  • Assuming exclusive lock can override shared lock immediately
  • Thinking shared lock releases automatically
  • Believing both locks can coexist on same data
4. You wrote this MySQL code to lock a table for writing but it causes an error:
LOCK TABLES my_table SHARE;

What is the error and how to fix it?
medium
A. Error: Missing semicolon. Fix: Add semicolon at end.
B. Error: Table does not exist. Fix: Create the table first.
C. Error: Invalid lock type 'SHARE'. Fix: Use 'WRITE' instead of 'SHARE'.
D. Error: Cannot lock table twice. Fix: Unlock before locking again.

Solution

  1. Step 1: Identify invalid lock keyword

    MySQL does not support 'SHARE' as a lock type; it uses 'READ' or 'WRITE'.
  2. Step 2: Correct the lock type

    To lock for writing, use LOCK TABLES my_table WRITE; instead.
  3. Final Answer:

    Error: Invalid lock type 'SHARE'. Fix: Use 'WRITE' instead of 'SHARE'. -> Option C
  4. Quick Check:

    Use WRITE for exclusive lock, SHARE is invalid [OK]
Hint: Use READ or WRITE only; SHARE is not valid in MySQL [OK]
Common Mistakes:
  • Using unsupported lock keywords
  • Ignoring error messages about syntax
  • Assuming SHARE is synonym for READ
5. You want to ensure that multiple users can read data from a table simultaneously, but only one user can update it at a time. Which locking strategy should you use in MySQL?
hard
A. Use shared locks (READ) for reading and exclusive locks (WRITE) for updating
B. Use exclusive locks (WRITE) for both reading and updating
C. Use shared locks (READ) for updating and exclusive locks (WRITE) for reading
D. Do not use any locks; rely on automatic commit

Solution

  1. Step 1: Understand shared and exclusive lock roles

    Shared locks allow multiple readers; exclusive locks allow one writer.
  2. Step 2: Apply correct locks for reading and writing

    Use shared locks for reading so many users can read safely, and exclusive locks for updating to prevent conflicts.
  3. Final Answer:

    Use shared locks (READ) for reading and exclusive locks (WRITE) for updating -> Option A
  4. Quick Check:

    Shared for read, exclusive for write ensures safe concurrency [OK]
Hint: Shared locks for reading, exclusive locks for writing [OK]
Common Mistakes:
  • Using exclusive locks for reading blocks all users
  • Using shared locks for writing causes conflicts
  • Ignoring locks and risking data corruption
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard