Geeks With Blogs

News This is the *old* blog. The new one is at blog.sixeyed.com
Elton Stoneman
This is the *old* blog. The new one is at blog.sixeyed.com

[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.

 

Posted on Wednesday, April 15, 2009 11:15 PM Code Snippet , WCF , Code Gallery | Back to top


Comments on this post: Managing Concurrency over Service Boundaries

# re: Managing Concurrency over Service Boundaries
Requesting Gravatar...
Left by robin on Mar 04, 2010 3:15 AM

# re: Managing Concurrency over Service Boundaries
Requesting Gravatar...
Its worthy as well as very important information. Doing great job to sharing it.
Left by Nutritionals555 on Jun 12, 2010 10:41 AM

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

# re: Managing Concurrency over Service Boundaries
Requesting Gravatar...
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.
Left by weight-loss on Oct 28, 2010 10:53 AM

# re: Managing Concurrency over Service Boundaries
Requesting Gravatar...
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.
Left by Turkish Gulet Cruises on Mar 08, 2011 6:03 AM

# re: Managing Concurrency over Service Boundaries
Requesting Gravatar...
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
Left by darren on May 04, 2011 2:37 PM

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

# re: Managing Concurrency over Service Boundaries
Requesting Gravatar...
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!
Left by forex trading on Jun 27, 2011 7:50 AM

# re: Managing Concurrency over Service Boundaries
Requesting Gravatar...
What if your computer have plenty of memory to perform multitask do you still need to implement incrementing lock sequence
Left by tech comm on Apr 23, 2012 10:15 PM

Your comment:
 (will show your gravatar)


Copyright © Elton Stoneman | Powered by: GeeksWithBlogs.net