Search code examples
sql-serversql-server-2008read-committed-snapshotsnapshot-isolation

Shared lock in READ_COMMITTED_SNAPSHOT and SNAPSHOT isolation


I've read on Microsoft's site http://msdn.microsoft.com/en-us/library/ms173763.aspx

that Sql Server doesn't request locks when reading data, except when a database is being recovered.

Does it mean that Sql Server using READ_COMMITTED_SNAPSHOT/SNAPSHOT ISOLATION doesn't use shared locks at all? How is that possible?

For example, if there are 2 transactions. First transaction T1 wants to update some row. Second transaction T2 starts reading same row (this transaction is copying him to some output buffer, response buffer or whatever it's called in Sql Server). At the same time transaction T1 starts updating that row (it created versioned row first).

Isn't there a possibility that transaction T2 will read uncommited data? Remember, transaction T2 started copying that row before T1 made update, so there is no exclusive lock on that row.

Is this situation even possible and how can this be avoided without setting shared lock on that row during copying of it's data?


Solution

  • Beside logical locks there are also physical latches to protect the database structures (particularly, in this example, pages). Latches protect any changes (modification of bits), irrelevant of isolation level. So even if the T1 does not acquire locks, it still needs to acquire a shared latch on the pages it reads, otherwise it would be victim to low level concurrent modifications done to the very structures it reads. T2 can modify the page containing the rows it modifies only if it obtains a page exclusive latch. Thus T1 can only see the image of the row either before T2 modified it (and therefore the row is the one T1 wants) or after T2 is complete with the modifications done to the row (and now T1 has to lookup the previous row image in the version store).

    The latching protocol must be honored by all isolation levels, including read uncommitted and versioned reads (ie. snapshot and friends).