Reputation: 1735
I am developing a shopping cart application for my client and am trying to find a strategy to make sure there is no way a collision can occur during purchasing.
For example, if there are five items left in inventory, and two clients happen to make a purchase at the same time; the inventory should be three items left and not four. It seems like I would have to know right before the purchase, what the current inventory is. Also, I need a way to tell if someone grabbed the last item even if they have not made the purchase yet.
What strategies/patterns should I use to ensure these conditions are met? I am developing a .net mvc application with SQL Server.
Upvotes: 1
Views: 419
Reputation: 30142
Ah concurrency. You have multiple things to consider here:
Your updates must:
There are various ways to do this but the above should work fine here. You can start the transaction in your code via a new transactionscope object or server side in a proc via begin transaction.
Upvotes: 2