如何实施股票发行系统,以确保使用C#和SQL Server不重复
我在C#API .NET CORE 6中实现了一个系统。数据库是SQL Server,我正在使用实体框架。我有一个单独的库存物品表(每个项目都有自己的记录)。当我出售商品时,我通过更改从null到dateTime.utcnow的“售出”字段的价值来记录其销售日期。当我需要出售商品时,我想在库存表中获得对该类别的一项的参考,这些项目与ctx.stock..stock..stock。 t.category == cat).first(),如果存在,它将返回我一个项目。然后,该项目已更新其“销售”字段。
由于系统是通过API访问的,因此似乎可以同时将代码称为两次(或更多),两个呼叫都将从ctx.stock.stock.where(t => t)返回同一项目。出售== null&& t.category == cat)。实际上2销售发生时,记录将被记录为出售。
现在,如果发生这种情况时,触发了DBUPDATECONCURRencyException,则可以避免这种情况。有人知道是否会这样吗?在这种情况下,我想可以实现重试循环,以尝试使用ctx.stock..there(t => t.sold == null&amp&& t.category == cat)。 )。这是最好的方法吗?
否则,将拥有一项单身服务,该服务在ctx.stock..stock.stock的运行中锁定(t => t.sold == null&& t.category == cat).first()和人口稠密的'savechanges''出售的田野是更好的选择?这可以确保没有可能是顺序的dbupdateconcurrencyexception,但是也许锁会成为问题。
I have a system implemented in C# API .Net Core 6. The database is SQL Server and I'm using Entity Framework. I have a table of individual stock items (each item has its own record). When I sell an item I record its sale date by changing the value of the 'Sold' field from NULL to DateTime.UtcNow. When I need to sell an item I want to get a reference to one of the items of that category in my stock table which I do with a ctx.Stock.Where(t=> t.Sold == null && t.Category==cat).First() that returns me a single item if it exists. That item then has its 'Sold' field updated.
Since the system is accessed via an API, it would seem possible that the code could be called twice (or more) at the same time, both calls would return the same item from the ctx.Stock.Where(t=> t.Sold == null && t.Category==cat).First() , both would then mark it as sold and when SaveChanges was called for both of them, it would save the later 'Sold' value and only 1 stock Record would be recorded as sold when in fact 2 sales had happened.
Now this could be avoided if the DbUpdateConcurrencyException is triggered when this type of situation occurs. Does anyone know if this would be the case? In that case I guess a retry loop could be implemented to attempt to get a new stock record using ctx.Stock.Where(t=> t.Sold == null && t.Category==cat).First() . Would this be the best way of doing it?
Otherwise would having a singleton service that has locks around the running of ctx.Stock.Where(t=> t.Sold == null && t.Category==cat).First() and SaveChanges of the populated 'Sold' field be a better bet? This could ensure there are no possible DbUpdateConcurrencyException as it would be sequential, but then maybe the locks would be a problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不太喜欢信任数据库。如果您想水平扩展应用程序并可以访问REDIS缓存,则可以使用 redlock a>以防止两个呼叫同时击中数据库。否则,只需使用一个简单的锁即可。该服务不必是单身人士...您只需要使用静态对象进行锁定即可。
I'm not much of a fan of trusting the database. If you want to be able to scale your app horizontally and have access to Redis Cache, you could use RedLock to prevent the two calls from hitting the DB at the same time. Otherwise just use a simple lock. The service doesn't have to be a singleton... you just need to use a static object for the locking.