Lab 08: Deadlocks & Detection
Step 1 — How Deadlocks Occur
Transaction A Transaction B
───────────────────────────── ─────────────────────────────
LOCK row 1 (success) LOCK row 2 (success)
LOCK row 2 → WAITING LOCK row 1 → WAITING
↑ ↑
└──────── DEADLOCK ──────────────┘Step 2 — MySQL Deadlock Settings
-- Check deadlock detection (default: ON)
SELECT @@innodb_deadlock_detect;
-- 1 = ON (immediate detection)
-- Lock wait timeout
SELECT @@innodb_lock_wait_timeout;
-- 50 seconds default
-- In high-throughput systems, you might set:
SET GLOBAL innodb_lock_wait_timeout = 5;
-- View recent deadlock
SHOW ENGINE INNODB STATUS\G
-- Look for: LATEST DETECTED DEADLOCK sectionStep 3 — Intentional Deadlock Demo (MySQL)
Step 4 — MySQL: Reading INNODB STATUS
Step 5 — PostgreSQL: Detecting Locks
Step 6 — PostgreSQL: Deadlock Demo
Step 7 — Deadlock Prevention Strategies
Strategy 1: Lock Ordering Convention
Strategy 2: Minimize Transaction Duration
Strategy 3: Use Advisory Locks
Step 8 — Capstone: Transfer Function with Deadlock Prevention
Summary
Topic
MySQL
PostgreSQL
Last updated
