Sql – NHibernate Concurrency Issue


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);

    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.

Best Solution

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.

Related Question