Junior — Middle
Explain what phantom reads are in the context of database transactions.
sobes.tech AI
Answer from AI
Phantom reads are a phenomenon in database transactions where a repeated query within the same transaction returns different sets of rows because another transaction has added or deleted rows that match the selection condition.
For example, transaction T1 reads all records with a price > 100. While it is running, another transaction T2 adds a new record with a price of 150 and commits the changes. If T1 repeats the query, it will see the new row — a "phantom."
This occurs at isolation levels below Serializable. To prevent phantom reads, the Serializable isolation level or special locking mechanisms are used.