views:

113

answers:

1

Over the weekend I realized that an application I'm working on which uses NHibernate as an ORM to a sqlite database has a concurrency issue.

I'm essentially looping through a collection in javascript and executing the following:

var item = new Item();
item.id = 1;
item.name = 2;
$.post("Item/Save", $.toJSON(item), function(data, testStatus) {
  /*User can be notified that the item was saved successfully*/
}, "text");

And my server code looks like this:

public ActionResult Save()
{
    string json = Request.Form[0];
    var serializer = new DataContractJsonSerializer(typeof(JsonItem));
    var memoryStream = new MemoryStream(Encoding.Unicode.GetBytes(json));
    JsonItem item = (JsonItem)serializer.ReadObject(memoryStream);
    memoryStream.Close();

    SaveItem(item);
    return Content("success");
}

The concurrency issue obviously occurs in the loop calling Save() for each element iterated, but I'm not sure how to accommodate for and prevent this. Any advice is appreciated.

+1  A: 

What is the concurrency issue?

I didn't understand your problem with concurrency.

Comment: if you iterate the collection, AND in the postback you reload the window... hmmm... there is a potential problem here. The first postback will throw away any pending work, refreshing completely the page.

Suggestion: don't iterate, send the complete collection in one Ajax call.

The postback wasn't actually there, I copied some old code from a textfile...oops. Anyway, I once upon a time thought that sending the entire collection would be a tricky procedure, but a half hour coding has shown me otherwise. Thanks for your solution.
splatto