A shared lock allows multiple transactions to read a resource at the same time but prevents any transaction from modifying it until all shared locks are released.
Click to reveal answer
beginner
What is an exclusive lock in a database?
An exclusive lock allows only one transaction to write or modify a resource, blocking other transactions from reading or writing it until the lock is released.
Click to reveal answer
beginner
Why do databases use shared and exclusive locks?
Databases use shared and exclusive locks to keep data consistent and avoid conflicts when multiple users access or change the same data at the same time.
Click to reveal answer
beginner
Can multiple transactions hold an exclusive lock on the same resource simultaneously?
No, only one transaction can hold an exclusive lock on a resource at a time to prevent conflicts during data modification.
Click to reveal answer
intermediate
What happens if a transaction tries to acquire an exclusive lock while shared locks are held on the same resource?
The transaction must wait until all shared locks are released before it can get the exclusive lock, ensuring no other transaction is reading the resource during modification.
Click to reveal answer
Which lock type allows multiple transactions to read a resource simultaneously?
AExclusive lock
BShared lock
CUpdate lock
DNo lock
✗ Incorrect
Shared locks allow multiple transactions to read the same resource at the same time.
What does an exclusive lock prevent?
AOther transactions from reading or writing the locked resource
BOnly other transactions from reading the resource
COnly other transactions from writing the resource
DNothing, it allows all access
✗ Incorrect
Exclusive locks block all other transactions from reading or writing the locked resource.
If a resource has shared locks, what must happen before an exclusive lock can be granted?
AOne shared lock must be converted to exclusive
BExclusive lock can be granted immediately
CAll shared locks must be released
DShared locks are ignored
✗ Incorrect
Exclusive locks require that no shared locks are held on the resource.
Which lock type is typically used when a transaction only needs to read data?
AShared lock
BExclusive lock
CDeadlock
DNo lock
✗ Incorrect
Shared locks are used to allow safe concurrent reads.
What is the main goal of using locks in a database?
ATo delete old data automatically
BTo speed up queries
CTo allow unlimited simultaneous writes
DTo keep data consistent and avoid conflicts
✗ Incorrect
Locks help maintain data consistency and prevent conflicts during concurrent access.
Explain the difference between shared and exclusive locks in a database.
Think about who can read or write when each lock is held.
You got /4 concepts.
Describe why databases need to use lock types like shared and exclusive locks.
Consider what happens if many users change data at the same time without locks.
You got /4 concepts.
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
Step 1: Understand shared lock behavior
A shared lock allows many users to read the same data at the same time safely.
Step 2: Compare with other lock types
Exclusive locks prevent others from reading or writing, but shared locks only prevent writing.
Final Answer:
Allow multiple users to read data simultaneously without conflicts -> Option A
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
Step 1: Recall MySQL lock syntax
In MySQL, to acquire a shared lock, you use LOCK TABLES table_name READ;.
Step 2: Eliminate incorrect options
WRITE is for exclusive locks, EXCLUSIVE and SHARE are not valid MySQL lock keywords.
Final Answer:
LOCK TABLES table_name READ; -> Option D
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
Step 1: Understand lock compatibility
Shared locks allow multiple readers but block exclusive locks until released.
Step 2: Analyze the scenario
Transaction B must wait for Transaction A to release the shared lock before getting exclusive lock.
Final Answer:
Transaction B waits until Transaction A releases the shared lock -> Option B
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
Step 1: Identify invalid lock keyword
MySQL does not support 'SHARE' as a lock type; it uses 'READ' or 'WRITE'.
Step 2: Correct the lock type
To lock for writing, use LOCK TABLES my_table WRITE; instead.
Final Answer:
Error: Invalid lock type 'SHARE'. Fix: Use 'WRITE' instead of 'SHARE'. -> Option C
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
Step 1: Understand shared and exclusive lock roles
Shared locks allow multiple readers; exclusive locks allow one writer.
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.
Final Answer:
Use shared locks (READ) for reading and exclusive locks (WRITE) for updating -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.