Wednesday, May 16, 2012

what is dirty read and phantom reads


DIRTY READS: Reading uncommitted modifications are call Dirty Reads. Values in the data can be changed and rows can appear or disappear in the data set before the end of the transaction, thus getting you incorrect or wrong data.
This happens at READ UNCOMMITTED transaction isolation level, the lowest level. Here transactions running do not issue SHARED locks to prevent other transactions from modifying data read by the current transaction. This also do not prevent from reading rows that have been modified but not yet committed by other transactions.
To prevent Dirty Reads, READ COMMITTED or SNAPSHOT isolation level should be used.

Consider T1 and T2 , two transactions

T1 reads some data ,
 T2 modify the same data but not yet committed the transaction
again T1 reads the uncommitted data ,
 this situation called as Dirty read

because u don't know , whether T2 can roll back or commit its changes

T1 is reading the uncommitted data , so this is called as READ UNCOMMITTED, this can be solved by READ COMMITED isolation level
**************************________________________****************************

PHANTOM READS: Data getting changed in current transaction by other transactions is called Phantom Reads. New rows can be added by other transactions, so you get different number of rows by firing same query in current transaction.


In REPEATABLE READ isolation levels Shared locks are acquired. This prevents data modification when other transaction is reading the rows and also prevents data read when other transaction are modifying the rows. But this does not stop INSERT operation which can add records to a table getting modified or read on another transaction. This leads to PHANTOM reads.



PHANTOM reads can be prevented by using SERIALIZABLE isolation level, the highest level. This level acquires RANGE locks thus preventing READ, Modification and INSERT operation on other transaction until the first transaction gets completed.




No comments:

Post a Comment