Database Management MySQL Subjective
Oct 01, 2025

What is a deadlock in MySQL and how to prevent it?

Detailed Explanation

Deadlock occurs when two transactions wait for each other to release locks.

Prevention strategies:
• Access tables in same order
• Keep transactions short
• Use appropriate isolation levels
• Use SELECT ... FOR UPDATE carefully
• Set innodb_lock_wait_timeout

Example:
-- Transaction 1: locks A then B
-- Transaction 2: locks B then A
-- Results in deadlock

MySQL automatically detects and rolls back one transaction.

Discussion (0)

No comments yet. Be the first to share your thoughts!

Share Your Thoughts
Feedback