简体   繁体   中英

Does transaction locks the row to prevent the data inconsistancy

I am new to MSSQL and creating a website where Customers can place orders.

Each order may have multiple items with any number of quantity.

I am interested in before saving my order to check if the desired quantity is available for each item, if yes then I will place the order and update the items inventory, otherwise I want to rollback.

But at the same time I want that any other order should wait till first transaction is finished. So that up updates don't overwrite the changes and produce inconsistency.

If each order is processed within a transaction, is it enough or do I have to consider something else too?

If you go on any Online retail website, you will notice that you go through the shop, buying stuff (not actually buying but being added to a basket) and once you have completed your shopping you go to Checkout that is, where you are asked to provide payment details etc.

So the idea is, the website shows everything (that has at least 1 stock item) to every customer, At this point no item inventory is being updated or inserted, at the checkout stage a complete order is compiled and submitted to system, (at this stage you will do the actual updates/inserts to item stock inventory) now how you want to handle the orders is entirely up to you.

  1. Do you want to rollback entire order when any one item has less stock than the quantity ordered?

  2. Do you want to commit all order lines and only rollback those order lines where the items has less stock than quantity ordered?

  3. Or do you want to place a provisional order regardless of the stock availability and manipulate the delivery date?

Depending on what path you chose to go with (this should be a business decision a developer shouldn't be making these decisions) there is a lot of flexibility, but one thing you never do is as soon as someone has select to buy an item, you update the inventory. All this should be done right in the end of the Purchase process and all should be done at Once.

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