简体   繁体   English

事务是否锁定行以防止数据不一致

[英]Does transaction locks the row to prevent the data inconsistancy

I am new to MSSQL and creating a website where Customers can place orders. 我是MSSQL的新手,正在创建一个可以让客户下订单的网站。

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. 所有这些都应该在购买过程的最后完成,并且应该一次完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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