开发者

asp.net mvc - racing condition : lock a method with same parameters

开发者 https://www.devze.com 2023-03-27 10:15 出处:网络
I am developing an asp.net mvc 3 web application. In my application I\'ve got a javascript client sending calls to my server.

I am developing an asp.net mvc 3 web application. In my application I've got a javascript client sending calls to my server. One of the calls is directed to a method of mine, that in turn:

  • Creates a new instance of an object -
  • Saves it to the database
  • Calls a remote web server.

I decide if to exit the function by querying my database to see if the object truly does exist (if so, I exit).

My problem is that if two calls are made just one after the other, the object hasn't had the chance to be saved to the database, and so both threads will think that they need to continue.开发者_开发技巧

What I'd like is to lock the method for only threads that are called with the same parameters. Using a lock on the entire method, or even parts of it is unacceptable, as it will cause severe performance issues.

How can this be done?


Use database transactions with serializable isolation level for this scenario. This should ensure the consistency of your read and insert operations.

0

精彩评论

暂无评论...
验证码 换一张
取 消