Locking, Isolation Levels, and Deadlocks
Understand DB2's locking model, the practical effect of each isolation level, and how deadlocks arise and get resolved.
DB2 locking is where concurrency theory meets real production incidents. The isolation level decides how much your transaction is protected, and what it costs everyone else.
Isolation levels are how tightly you reserve a shared meeting room: Uncommitted Read is walking in without knocking and reading whatever's currently on the whiteboard, even mid-erase; Repeatable Read is locking the whole room for your entire meeting so nobody else can touch anything until you leave; Cursor Stability is holding the door only while you're actually looking at one specific note. A deadlock is two people each holding a door the other needs open, until security (DB2's lock manager) steps in and asks one of them to leave.
Key Concepts
RR Repeatable Read locks every row read, held to commit.
Nothing you read can change. Most blocking.
RS Read Stability locks the rows you qualified, held to commit.
New rows can still appear.
CS Cursor Stability locks the row only while the cursor sits on
it. The usual default.
UR Uncommitted Read no locks taken, none waited for.
Can read uncommitted data. transaction A: locks row 1, then wants row 2
transaction B: locks row 2, then wants row 1 deadlock a genuine cycle; one side is chosen and rolled back
timeout no cycle; the waiter gave up