25.6 Deadlocks: Detection, Error Handling, and Prevention Patterns
Right, so you’ve gotten your locks all lined up, and now everything has ground to a halt. Welcome to the deadlock, the database equivalent of a standoff in a Western movie, but with less dramatic music and more error logs. A deadlock happens when two or more transactions are each waiting for the other to release a lock, creating a perfect circle of pointless waiting. The database isn’t stupid; it won’t let this nonsense continue forever. One of you is going to get shot (figuratively, your transaction will be rolled back) so the other can live. Your job is to make sure it’s not your transaction that gets chosen and, more importantly, to write your code so these standoffs are rare and handled gracefully when they do occur.