In database systems that handle many simultaneous users or processes, conflicts over shared data are inevitable. When multiple transactions need access to the same rows or tables, the database uses locks to keep the data consistent. Most of the time, these locks are managed smoothly – one transaction waits, another finishes, and everything moves on.
But sometimes, two transactions end up waiting on each other’s locked resources, and neither can proceed. This situation is known as a deadlock. It’s a subtle but important problem in RDBMSs, because even though databases can detect and resolve deadlocks automatically, they can still cause errors, slow performance, and application frustration if not properly understood or prevented.
This article explores what a deadlock is in SQL, how it occurs, how RDBMSs detect and resolve it, and the best practices to prevent it.