Challenge - 5 Problems
Deadlock Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate2:00remaining
What is a deadlock in database systems?
In simple terms, what does a deadlock mean when two or more transactions are running in a database?
Attempts:
2 left
💡 Hint
Think about what happens when two people each hold a key the other needs and neither can proceed.
✗ Incorrect
A deadlock happens when two or more transactions wait for each other to release locks, so none can continue. This causes the system to freeze those transactions until resolved.
❓ query_result
intermediate2:00remaining
Which query order can cause a deadlock?
Given two transactions:
Transaction 1 locks Table A then Table B.
Transaction 2 locks Table B then Table A.
Which option shows the order of locking that can cause a deadlock?
Transaction 1 locks Table A then Table B.
Transaction 2 locks Table B then Table A.
Which option shows the order of locking that can cause a deadlock?
Attempts:
2 left
💡 Hint
Deadlocks happen when each transaction holds a lock the other needs next.
✗ Incorrect
Deadlocks occur when Transaction 1 locks Table A and waits for Table B, while Transaction 2 locks Table B and waits for Table A. Both wait forever.
📝 Syntax
advanced2:00remaining
Which SQL statement helps prevent deadlocks by setting lock timeout?
Choose the correct SQL command to set a lock timeout to avoid waiting forever and reduce deadlocks.
Attempts:
2 left
💡 Hint
The correct syntax uses underscore and no equals sign.
✗ Incorrect
In SQL Server, the correct syntax to set lock timeout is SET LOCK_TIMEOUT 5000; which sets timeout to 5000 milliseconds.
❓ optimization
advanced2:00remaining
Which approach best reduces deadlocks in concurrent transactions?
You want to minimize deadlocks in your database where many transactions update multiple tables. Which approach is best?
Attempts:
2 left
💡 Hint
Think about how consistent order prevents waiting cycles.
✗ Incorrect
Accessing tables in the same order prevents circular waiting, which is a main cause of deadlocks.
🔧 Debug
expert3:00remaining
Identify the cause of deadlock in this SQL transaction sequence
Two transactions run:
Transaction 1:
BEGIN TRAN;
UPDATE Accounts SET balance = balance - 100 WHERE id = 1;
WAITFOR DELAY '00:00:05';
UPDATE Accounts SET balance = balance + 100 WHERE id = 2;
COMMIT;
Transaction 2:
BEGIN TRAN;
UPDATE Accounts SET balance = balance + 100 WHERE id = 2;
WAITFOR DELAY '00:00:05';
UPDATE Accounts SET balance = balance - 100 WHERE id = 1;
COMMIT;
What causes the deadlock here?
Transaction 1:
BEGIN TRAN;
UPDATE Accounts SET balance = balance - 100 WHERE id = 1;
WAITFOR DELAY '00:00:05';
UPDATE Accounts SET balance = balance + 100 WHERE id = 2;
COMMIT;
Transaction 2:
BEGIN TRAN;
UPDATE Accounts SET balance = balance + 100 WHERE id = 2;
WAITFOR DELAY '00:00:05';
UPDATE Accounts SET balance = balance - 100 WHERE id = 1;
COMMIT;
What causes the deadlock here?
Attempts:
2 left
💡 Hint
Look at the order of account updates and the delay between them.
✗ Incorrect
Transaction 1 locks account 1 then waits before locking account 2. Transaction 2 locks account 2 then waits before locking account 1. This causes a deadlock.