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.
精彩评论