1.

How do you check the rows affected as part of previous transactions?

Answer»

SQL standards state that the following three phenomena should be prevented whilst concurrent transactions. SQL standards define 4 levels of TRANSACTION isolations to deal with these phenomena.

  • Dirty reads: If a transaction reads data that is written DUE to concurrent uncommitted transaction, these reads are called dirty reads.
  • Phantom reads: This occurs when two same queries when EXECUTED separately return different rows. For example, if transaction A retrieves some set of rows matching search criteria. Assume another transaction B retrieves new rows in addition to the rows obtained earlier for the same search criteria. The results are different.
  • Non-repeatable reads: This occurs when a transaction tries to read the same row multiple times and gets different values each time due to CONCURRENCY. This happens when another transaction updates that data and our current transaction fetches that updated data, resulting in different values.

To tackle these, there are 4 standard isolation levels defined by SQL standards. They are as follows:

  • Read Uncommitted – The lowest level of the isolations. Here, the transactions are not isolated and can read data that are not committed by other transactions resulting in dirty reads.
  • Read Committed – This level ensures that the data read is committed at any instant of read time. Hence, dirty reads are avoided here. This level makes use of read/write lock on the current rows which prevents read/write/update/delete of that row when the current transaction is being operated on.
  • Repeatable Read – The most restrictive level of isolation. This holds read and write locks for all rows it operates on. Due to this, non-repeatable reads are avoided as other transactions cannot read, write, update or delete the rows.
  • Serializable – The HIGHEST of all isolation levels. This guarantees that the execution is serializable where execution of any concurrent operations are guaranteed to be appeared as executing serially.

The following table clearly explains which type of unwanted reads the levels avoid:

Isolation levels Dirty Reads Phantom Reads Non-repeatable reads
Read Uncommitted Might occurMight occurMight occur
Read Committed Won’t occurMight occurMight occur
Repeatable ReadWon’t occurMight occurWon’t occur
SerializableWon’t occurWon’t occurWon’t occur


Discussion

No Comment Found