Elton Stoneman

  Home  |   Contact  |   Syndication    |   Login
  120 Posts | 0 Stories | 3598 Comments | 0 Trackbacks

News

Archives

Post Categories

[Source: http://geekswithblogs.net/EltonStoneman]

Managing concurrency within an application boundary can be straightforward where you own the database schema and the application's data representation. By adding an incrementing lock sequence to tables and holding the current sequence in entity objects, you can implement optimistic locking at the database level without a significant performance hit. At the service level, the situation is more complicated. Even where the database schema can be extended, you wouldn't want the internals of concurrency management to be exposed in service contracts, so the lock sequence approach isn't suitable.

An alternative pattern is to compute a data signature representing the retrieved state of an entity at the service level, and flow the signature alongside the entity in Get services. On Update calls, the original data signature is passed back and compared to the current signature of the data; if they differ then there's been a concurrency violation and the update fails. The signature can be passed as a SOAP header across the wire so it's not part of the contract and the optimistic locking strategy is transparent to consumers.

The level of transparency will depend on the consumer, as it needs to retrieve the signature from the Get call, retain it, and pass it back on the Update call. In WCF the DataContract versioning mechanism can be used to extract the signature from the header and retain it in the ExtensionData property of IExtensibleDataObject. The contents of the ExtensionData property are not directly accessible, so if the same DataContract is used on the Get and the Update, and the signature management is done through WCF extension points, then concurrency control is transparent to users.

I've worked through a WCF implementation for this pattern on MSDN Code Gallery here: Optimistic Locking over WCF. The sample uses a WCF behavior on the server side to compute a data signature (as a hash of the serializable object – generating a deterministic GUID from the XML string) and adds it to outgoing message headers for all services which return a DataContract object. On the consumer side, a parallel behaviour extracts the data signature from the header and adds it to ExtensionData, by appending it to the XML payload and using the standard DataContractSerializer to extract it.

The update service checks the data signature passed in the call with the current signature of the object and throws a known FaultException if there's been a concurrency violation, which the WCF client can catch and react to:

Sixeyed.OptimisticLockingSample

The sample solution consists of four projects providing a SQL database for Customer entities, WCF Get and Update services, a WCF client and the ServiceModel library which contains the data signature behaviors. DataSignatureServiceBehavior adds a dispatch message formatter to each service operation, which computes the hash for any DataContract objects being returned, and adds it to the message headers. DataSignatureEndpointBehavior on the client adds a client message formatter to each endpoint operation, which extracts the hash from incoming calls, stores it in ExtensionData and adds it back to the header on outgoing calls.

Concurrency checking is done on the server side in the Update call, by comparing the given data signature to the signature from the current object state:

Guid dataSignature = DataSignature.Current;

if (dataSignature == Guid.Empty)

{

//this is an update method, so no data signature to

//compare against is an exception:

throw new FaultException<NoDataSignature>(new NoDataSignature());

}

 

Customer currentState = CustomerEntityService.Load(customer);

Guid currentDataSignature = DataSignature.Sign(currentState);

//if the data signatures match then update:

if (currentDataSignature == dataSignature)

{

CustomerEntityService.Update(customer);

}

else

{

//otherwise, throw concurrency violation exception:

throw new FaultException<ConcurrencyViolation>(new ConcurrencyViolation());

}

A limitation of the sample is the use of IExtensibleDataObject to store the data signature at the client side. Although this is fully functional and allows a completely generic solution, it relies on reflection to extract the data signature and add it to the message headers for the update call, which is a brittle option. Where you have greater control over the client, you can use a custom solution which will be more suitable – e.g. creating and implementing an IDataSignedEntity interface, or if consuming the services in BizTalk, by using context properties.

 

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati
posted on Wednesday, April 15, 2009 11:15 PM

Feedback

# re: Managing Concurrency over Service Boundaries 3/4/2010 3:15 AM robin
Nice Post!Data Recovery Software|Guard Process

# re: Managing Concurrency over Service Boundaries 6/12/2010 10:41 AM Nutritionals555
Its worthy as well as very important information. Doing great job to sharing it.


# re: Managing Concurrency over Service Boundaries 6/14/2010 3:08 AM BenchCraft22
Subject of this post is very interested.I am very impressed with the article I have just read.Thanks a lot fo sharing.

# re: Managing Concurrency over Service Boundaries 10/28/2010 10:53 AM weight-loss
It is the purpose of this chapter to describe the requirements and procedures for determining the consistency of proposed development projects with the city comprehensive plan, including meeting the concurrency management provisions of the comprehensive plan.

# re: Managing Concurrency over Service Boundaries 3/8/2011 6:03 AM Turkish Gulet Cruises
Very good tips...It's provided a couple but it would be nice if you went into a little more detail about it.. Keep blogging.

# re: Managing Concurrency over Service Boundaries 5/4/2011 2:37 PM darren
This is really great guide. Good thing that I have find your site.. I wish to see more on your updates. Cursos de ingles en el extranjero


# re: Managing Concurrency over Service Boundaries 6/11/2011 10:21 AM no medical life insurance
I must say that elements you put here look awesome..I’ll be checking in on a regularly now….Keep up the good work!


# re: Managing Concurrency over Service Boundaries 6/27/2011 7:50 AM forex trading
This is one of the most incredible blogs Ive read in a very long time.  The amount of information in here is stunning, like you practically wrote the book on the subject.  Your blog is great for anyone who wants to understand this subject more.  Great stuff; please keep it up!

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: