Share via


Concurrency in SSDS

A common concern with SSDS, and a common question I get in many presentations I've given is how to handle concurrency and entity versioning.

Suppose you have the following sequence of events:

 

image

By default, SSDS will just accept the last Update and overwrite any changes made in between. If you want SSDS to be strict about versioning, then you need to express this intent in the scope of the call.

If you are using SOAP, you just need to create a new instance of the VersionMatch class and specify your requirement:

 public void Update(T entity)
{
     try
     {
         Scope scope = CreateScope();
         scope.VersionMatch = new VersionMatch() 
         { 
            MatchType = VersionMatchType.Match, 
            Version = entity.Version 
         };
         
          scope.EntityId = entity.Id.ToString();
         Entity flexibleEntity = entityMapper.FromType(entity);
         proxy.Update(scope, flexibleEntity);
     }
     catch (FaultException<Error> ex)
     {
        throw new UpdateException(ex);
     }
}

A side note: in writing a new test to verify this behavior, I dumped ExceptionExpected attribute in the test method altogether for this one, that gives me much more control on the exact place I expect the exception to occur:

 Assert.IsTrue(ThrowsException<UpdateException>(() => rb.Update(b2)));

ThrowsException<E> is:

      private static bool ThrowsException<E>(Action f) where E : Exception
     {
            try
            {
                f();
            }
            catch (E)
            {
                return true;
            }
            catch
            {
            }

            return false;
     }
  

Comments

  • Anonymous
    August 05, 2008
    EP: I assume that the ETag does the same thing in HTTP as the VersionMatch class in SOAP, right?

  • Anonymous
    August 08, 2008
    Yes it does. See: http://blogs.msdn.com/jcurrier/archive/2008/07/25/sql-server-data-services-upgrade-has-been-completed.aspx

  • Anonymous
    October 07, 2008
    The comment has been removed

  • Anonymous
    October 12, 2008
    I also try to use the ThrowsException<E>(Action f) "pattern". It does work fine when running in "run" mode. But it does not work using the "debug" mode. Do you have the same experience or could you give me a hint! Thankx, cheers Harry