Junior — Middle
Can you explain what phantom reads are in the context of transaction management?
sobes.tech AI
Answer from AI
Phantom read is a phenomenon in transaction management where, within a single transaction, a repeated query to the database returns different sets of rows due to changes made by other transactions.
For example, transaction T1 reads a set of rows satisfying a condition (e.g., all orders for today). Between two reads, another process (transaction T2) adds new rows that also satisfy the condition. On the second read, "phantom" rows appear in T1 — new records that were not present during the first read.
This occurs due to the absence of range locks or low isolation levels of transactions (e.g., READ COMMITTED). To prevent phantom reads, stricter isolation levels like SERIALIZABLE are used, or range locking mechanisms.