Search code examples
c#silverlight-4.0wcf-ria-services

Are there really any concurrency issues with RIA?


In one of the videos of RIA services of Pluralsight I was referring, they had an update code like this:

public void UpdateProspect(Prospect currentProspect)
{
    currentProspect.LastUpdate = DateTime.Now;
    ObjectContext.Prospects.AttachAsModified(currentProspect, ChangeSet.GetOriginal(currentProspect));
}

My first question is how will this update cause problems? To show the problems he starts two Silverlight clients, then from first client he updates the item and it updates nicely. Then he goes to the second client and makes an update and it throws an error.

Why is RIA throwing errors? What has it got to do with the first update? This is really buggy I think and we need to again write some special code to resolve the EntityConflict and then again submit the batch to the server. Is this appropriate?

He also set the ConcurrencyMode to Fixed. My second question is when would you set the ConcurrencyMode to Fixed? By default, the mode is set to None.


Solution

  • This is because of a race condition caused by a Stale Read. Consider the following:

    1. Two clients get a copy of the Prospect - lets call it version 1
    2. A modifies the LastUpdate property, the object is now version 2
    3. A saves the Prospect
    4. B modifies the LastUpdate property. To Client B, the version is now version 2 but Client B's version 2 is different from Client A's
    5. B saves the Prospect - this would overwrite Client A's changes!

    The problem here is that B can't know that A has made any changes. The Exception is thrown to prevent the race condition from accidentally deleting data.

    The solution is to catch this exception and report that the Prospect was changed while you were editing it, and then reload it.

    You can find more information about using ConcurrencyMode=fixed here.