I am working on a multithreaded application (C#), and 2 threads are updating a table using NOLOCK at the same time, is that a problem? To be more specific, they are both updating the same records.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The answer is “it depends”.
NOLOCK allows ‘dirty reads’. This means that within a transaction, you may see uncommitted data from another transaction. If you have multiple threads updating the same row in the same table, you may see the modified value of data that the other thread touched, before that thread commits it’s transaction.
For example, take the table account_balances, defined as (account_num int, balance decimal(12,2)). Let’s assume the following happens:
// precondition, account #1 has a balance of 10.00
// The account balance is now -5, even though it should be 5.
What you won’t see is some form of inconsistent data within a field- the nolock hint isn’t like running multi-threaded code without a lock- individual writes are still atomic.