Reputation: 151
Now I am trying to lock an item in the AppFabric Cache, and I want to hold the lock until my work is done, so I need to tell AppFabric don't let my lock be timeout when I have already owned the lock. My solution is to lock the the same item periodically even I have already owned the lock.
while (true)
{
try
{
String value = cacheClient.GetAndLock("key1", TimeSpan.FromSeconds(10), out lockHandle, true) as String;
Console.WriteLine(value);
}
catch (DataCacheException e)
{
switch (e.ErrorCode)
{
case DataCacheErrorCode.KeyDoesNotExist:
Console.WriteLine("Key not found");
break;
case DataCacheErrorCode.ObjectLocked:
Console.WriteLine("Object Locked");
break;
default:
Console.WriteLine("Others " + e.ErrorCode);
break;
}
}
Thread.Sleep(1000);
}
However there is a problem. The code is throwing a DataCacheException and the ErrorCode is ObjectLocked. That makes me unable to know if I still have the lock, because if the lock owned by another client, the exception is the same as the current one. Does anyone have a solution to solve the problem?
Thank you in advance.
Upvotes: 3
Views: 514
Reputation: 24558
In the pessimistic concurrency model, a client explicitly locks an object to perform operations. When objects are locked, a lock handle is returned as an output parameter and is required to unlock the object. There a only one unique handle per locked object.
Locked objects in the cache can still be replaced by any cache client with the Put method and you can get items with Get. Cache-enabled applications are responsible for consistently using PutAndUnlock for items that use the pessimistic concurrency model.
So in your case, if you want to increase to lock timeout of a locked object, you can simply Put again the same object with the same timeout (by the same client, the "locker")
Upvotes: 1