Sunday, 15 January 2012

MS SQL Server Isolation level explained


An isolation level determines the degree of data isolation between concurrent transactions. The default SQL Server isolation level is Read Committed. 

In the ascending order of isolation: Read Uncommitted, Read Committed, Repeatable Read, Serializable.

Read uncommitted: dirty read, you can read those that have been modified by other transaction but not yet committed. It does not issue shared locks to prevent other transactions from modifying data read by the current transaction.

Read committed: cannot read data that has been modified but not committed by other transactions, this prevents dirty reads. Data can be changed by other transactions between individual statements within the current transaction, resulting nonrepeatable reads or phantom data. Shared locks are only held for individual statements.

Repeatable read: cannot read data that has been modified but not yet committed by other transactions and that no other transactions can modify data that has been read by current transaction until current transaction completes. Shared locks are placed on all data read by each statement in the transaction and are held until the transaction completes. This prevents other transactions from modifying any rows that have been read by the current transaction. But other transactions can insert new rows, so the query statement of current transaction could return unrepeatable results. Shared locks are held to the end of a transaction instead of being released at the end of each statement as READ COMMITTED.

Serializable: is the highest level, where transactions are completely isolated from one another. It cannot read data that has been modified but not yet committed by other transactions. No other transactions can modify data that has been read by the current transaction until the current transaction completes. Also, other transactions cannot insert rows with key values that would fall in the range of keys read by any statements in the current transaction until current transaction completes. Range locks are placed in the range of key values that match the search conditions of each statement executed in the transaction, this means that if any of the statements in the current transaction are executed a second time, they will read the same set of rows. The range locks are held until the transaction completes.

No comments:

Post a Comment