Reputation: 3526
How can I acquire an Update, Exclusive lock? Article states that “a user can ask for an Update lock instead of the Shared lock” but I didn't find a way to specify type of lock.
Currently I have a dictionary that stores lists. My goal is: when someone only reads the list — let's take a snapshot and don't block anyone else. When somebody is going to modify the list — let's take an Exclusive lock or maybe Update lock (honestly I can't find a difference) and everybody else should wait before transaction commits, even reads should not be possible.
The detail that I'm failing to understand is: how to specify that if intention is only to read, than allow read of the collection, and when intention is to read and then write(we are not able to alter existing data in other way), then this transaction should wait all locks and only then successfully read and modify the collection.
Upvotes: 1
Views: 620
Reputation: 3526
This is how I solved my issue with reading and writing at the same time.
Update lock does not work as I expected, it takes Exclusive lock ALWAYS and it is not possible to perform any other operation on collection at this moment (including just reading) if you are going to update an item of the same key.
But it is possible to make read operation lock-free — you just need to read from secondary replicas. Of course in that case you probably will not get actual data, but it is still better than nothing and TimeoutException.
Still, simultaneous writing to the same item is pretty slow and you need to think carefully about retry policy if you want update data even with TimeoutException.
Upvotes: 1
Reputation: 275
There's an enum called LockMode that you can pass to some methods on reliable dictionaries and queues that allow you to specify the functionality desired. Afaik, the only methods that support it are TryGetValue and ContainsKey on dictionaries and TryPeek on queues. I've been confused by that article myself, as I was originally lead to believe that I could specify it per transaction (which would be awesome btw). At the moment your options with LockMode are rather limited.
Upvotes: 2