I am taking an operating systems class where we just learned about the ‘readers and writers’ problem: how do you deal with multiple processes that want to read and write from the same memory (at the same time)? I’m also dealing with a version of this problem at work: I am writing an application that requires multiple users to read and write to a shared SQL server database. Because the ‘readers and writers’ problem seems so well understood and discussed, I’m assuming that Microsoft has solved it for me. Meaning, that I don’t need to worry about setting permissions or configuring SQL server to ensure that people are not reading and writing to a database at the same time. Specifically, can I assume that, with SQL server 2005, by default:
- No process reads while another process is writing
- No two processes write at the same time
A writer will take an exclusive
Xlock on at least the row(s) they are modifying and will hold this until their transaction commits.Xlocks are incompatible with otherXlocks so two writers can not concurrently modify the same row.A reader will (at default read committed isolation level) take a shared lock and release this as soon as the data is read. This is incompatible with an
Xlock so readers must wait for writing transactions to finish before reading modified data. SQL Server also has snapshot isolation in which readers are not blocked by writers but instead read an earlier version of the row.