If there is a lock on an adjacent key in an indexed table, an insert operation will fail when DB2 uses which isolation level?

Difficulty: Medium

Correct Answer: Repeatable read isolation

Explanation:


Introduction / Context:
This question examines how isolation levels interact with next key or adjacent key locking in DB2. When transactions operate under certain isolation levels, the database may lock ranges of keys in an index to prevent phantom rows or inconsistent reads. These locks can affect other operations, such as inserts that try to place a new key in the locked range.


Given Data / Assumptions:

    There is an index on the table and the database uses key based locking.

    An adjacent key lock exists due to another transaction.

    The operation in question is an insert that would place a key in the locked range.

    Different isolation levels impose different locking strengths.


Concept / Approach:
Repeatable read isolation aims to prevent both non repeatable reads and phantoms by holding locks on all rows accessed, and often on ranges of keys. To guarantee that new rows do not appear that would change the result of a query, the engine may lock not only existing keys but also gaps between keys. If another transaction holds an adjacent key lock under repeatable read, inserts into that range may be blocked or fail until the lock is released.


Step-by-Step Solution:
1. Recall that repeatable read is one of the strongest isolation levels and uses tight locking to prevent phantoms. 2. Understand that adjacent key locking is related to range locks on index entries. 3. When an adjacent key is locked under repeatable read, an insert that would create a new key in that range cannot proceed. 4. Committed read generally uses less restrictive locks and may permit more concurrent inserts. 5. Share read and indexed read are not standard DB2 isolation levels and do not describe this behavior, so repeatable read is the correct answer.


Verification / Alternative check:
DB2 documentation on isolation levels and key range locking explains that repeatable read uses next key locks to prevent phantoms, which can in turn block inserts into locked ranges. Testing with concurrent transactions in a controlled environment can demonstrate this effect.


Why Other Options Are Wrong:
Committed read provides weaker guarantees and typically does not hold range locks for the entire transaction, so inserts are less likely to fail due to adjacent key locks.
Share read and indexed read are not standard DB2 isolation levels and are presented here as distractors rather than real options.
Therefore they do not accurately describe isolation level behavior.


Common Pitfalls:
A frequent mistake is to assume that all isolation levels behave the same with respect to locking. Another pitfall is ignoring the impact of range locks on insert and delete operations. When designing applications that run under repeatable read, developers must anticipate increased contention on hot index ranges.


Final Answer:
An insert can fail because of an adjacent key lock when the isolation level is repeatable read.

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion