简体   繁体   中英

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

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 technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM