3

Imagine a little webshop which uses its own currency.

In the database, there is a table for items, which can be bought. The count of each item is limited. This limit is stored in a collumn in this table as integer.

Also, there is a table for the user's cash accounts, whereas for each account the current balance is saved.

If, for example, two users conduct their purchases at the same time and only one item is available, it could be possible that both users pay but only one receives the item due to racing conditions.

How can such racing conditions be resolved, without relying on entity framework throwing exceptions on saving?

How can I ensure the count of available items and the account balance of the buyer is correctly updated?

4

1 回答 1

2

这并不是实体框架所特有的问题,它几乎适用于任何商店场景。这归结为一个政策问题 - 确保两个客户不购买同一商品的唯一方法是在他们将商品添加到购物车或开始结账过程时允许在该商品上放置临时锁,类似于音乐会门票或航班的销售方式。如果在设定的时间内未完成购买,此锁定将过期,并且该项目将被释放以供其他客户购买。

在电子商务环境中,这不太合适,因为人们可能会将商品添加到购物车而不结账,或者花费额外的时间来选择其他商品。这可能会导致您有待售物品但无法购买,因为它们在某人的购物车中,而该人不打算结账。相反,允许重复订单,但付款通常只经过预授权,然后在发货或订单确认时完成,因此即使第二位客户输入所有详细信息并按购买,他们的卡也不会被收取费用,因为项目将无法发货。

您可以在结帐过程中的不同阶段实施检查,以确保购物车中的物品仍然可用,或者在最简单的级别,将其留给最后一页上的最终“立即付款”按钮。最终,这只是减少了竞争条件的可能性,而不是消除它。

于 2013-01-05T11:34:15.820 回答