Thursday, March 5, 2020

SQL Transactions and Concurrency Control


  • P1 (Dirty read): Transaction T1 modifies a row. Transaction T2 then reads that row before T1 performs a COMMIT WORK.If T1 then performs a ROLLBACK WORK, T2 will have read a row that was never committed, and so may be considered to have never existed.
  • P2 (Nonrepeatable read): Transaction T1 reads a row. Transaction T2 then modifies or deletes that row and performs a COMMIT WORK. If T1 then attempts to reread the row, it may receive the modified value or discover that the row has been deleted.
  • P3 (Phantom): Transaction T1 reads the set of rows N that satisfy some . Transaction T2 then executes statements that generate one or more rows that satisfy the used by transaction T1. If transaction T1 then repeats the initial read with the same , it obtains a different collection of rows.


Serializable

With a lock-based concurrency control DBMS implementation, serializability requires read and write locks (acquired on selected data) to be released at the end of the transaction. Also range-locks must be acquired when a SELECT query uses a ranged WHERE clause, especially to avoid the phantom reads phenomenon.\

Repeatable reads

In this isolation level, a lock-based concurrency control DBMS implementation keeps read and write locks (acquired on selected data) until the end of the transaction. However, range-locks are not managed, so phantom reads can occur.

Read committed

In this isolation level, a lock-based concurrency control DBMS implementation keeps write locks (acquired on selected data) until the end of the transaction, but read locks are released as soon as the SELECT operation is performed (so the non-repeatable reads phenomenon can occur in this isolation level). As in the previous level, range-locks are not managed.
Putting it in simpler words, read committed is an isolation level that guarantees that any data read is committed at the moment it is read. It simply restricts the reader from seeing any intermediate, uncommitted, 'dirty' read. It makes no promise whatsoever that if the transaction re-issues the read, it will find the same data; data is free to change after it is read.

Read uncommitted

This is the lowest isolation level. In this level, dirty reads are allowed, so one transaction may see not-yet-committed changes made by other transactions.

Reference: https://en.wikipedia.org/wiki/Isolation_(database_systems)

No comments: