Wednesday, December 30, 2009

LINQ to SharePoint [Concurrency Problem] and how to Resolve .

What is Concurrency ?

The situation in which two or more users at the same time try to update the same database row, here in SharePoint row as list item column value . Like LINQ to SQL  LINQ to SharePoint also supports optimistic concurrency control.

In the LINQ to SQL object model, an optimistic concurrency conflict occurs when both of the following conditions are true:

  • The client Apps tries to submit changes to the database( In SharePoint list) .

  • One or more update-check values have been updated in the database since the client last read them.

Here we are going to test this issue and see how we can solve this conflicts.

1) Create a console application  and add Generated DataContext Class which is created using SPMETAL.EXE.

2) Now using LINQ to SharePoint query the custom list  where Title is Manager and update a Title column value as user inputted (Read from console apps) .

3) Now execute the two instance of same console App and test it .



Execute two instance of the above code.


First press enter in above App Instance 1 this will update the SharePoint list as shown below(see selected list ).


Now Press Enter in above App Instance 2 . Now you will get the following Exception .


Let’s modify the Console application to handle the above exception.





RefreshMode.KeepCurrentValues will overwrite the entity values in the list with the data stored in your current entity in memory, including the original value that you didn’t change; an alternative solution would have been to merge your changes with the current values of the database by using RefreshMode.KeepChanges. refer this URL for Optimistic concurrency overview. 



No comments: