简体   繁体   English

如何解决销售数据库中的并发事务问题

[英]How to resolve concurrent transaction issues in sales database

My application is storing some data in SQL Table called Transactions. 我的应用程序是将一些数据存储在名为Transactions的SQL表中。 Users are able to sell something from Client1 to Client2 and then Client2 has it. 用户可以从Client1到Client2销售一些东西,然后Client2就可以销售。 I am storing it in a table the following way: 我将它以下列方式存储在表中:

Client 1 | Buy  | Something | Price  | External |
Client 1 | Sell | Something | Price2 | Client 2 |
Client 2 | Buy  | Something | Price2 | Client 1 |

First Client1 bought it (brought it in or just has it for all it matters). 第一个Client1买了它(把它带进来或者只是让它完全重要)。 Then he sells it to another client. 然后他把它卖给另一个客户。

And all that is fine, it works but my application has a short time when it doesn't check if Client 1 still has what it claims to have (when data is loaded into gui). 一切都很好,它可以工作,但我的应用程序有一个很短的时间,它不检查客户端1是否仍然具有它声称拥有的内容(当数据加载到gui时)。 So if 2 users would make run for it it's possible that product from Client1 could be sold multiple times. 因此,如果有2个用户可以运行它,那么Client1的产品可能会被多次出售。 Most likely it won't happen since my users tend to share their work on what they are doing but there's always BUT... 最有可能它不会发生,因为我的用户倾向于分享他们正在做的事情的工作,但总是但......

How to prevent this? 怎么预防这个? Would simple select query check just before insert transaction be sufficient or should this be done differently? 在插入事务就足够之前进行简单的选择查询检查还是应该以不同的方式完成? (I can imagine situation when multiple people make run for it and some would succeed). (我可以想象多个人为此奔跑而有些人会成功的情况)。 How is this handled in real world situations on heavy systems? 在重型系统的实际情况下如何处理? Like when you pick money from one bank account with 2 cards from 2 different CashMachines (although i believe they would just let balance go under 0 in this case even if it wouldn't be allowed). 就像当你从两个不同的CashMachines的2张牌中从一个银行账户中选择钱时(虽然我相信他们只会让平衡在这种情况下低于0,即使不允许)。

So what options do I have? 那么我有什么选择? What is your take on this? 你对此有什么看法?

I assume this is not a real inventory tracking system, where the database merely record real world events, but instead is a some virtual auction or market place sort of application, where 'reality' is whatever the application believe it is. 我认为这不是一个真正的库存跟踪系统,数据库只是记录现实世界的事件,而是一些虚拟拍卖或市场类应用,其中“现实”是应用程序认为的任何东西。

If you only keep history, you'll never have the current state. 如果你只记录历史,你将永远不会拥有当前状态。 W/oa current state, you can't make efficient correctness decisions. 没有当前状态,你无法做出有效的正确性决定。 So keep a current state. 所以保持现状。 Have a table of items and their current owner. 有一个项目表和他们当前的所有者。 The problem you ask then become a simple problem of 'How to I prevent a lost update?' 那么你问的问题就变成了“如何防止丢失更新?”的简单问题。 or 'How do I prevent a blind write'? 或'我如何防止盲写'? (ie. a write-write conflict) and the answer is well known in database application: use optimistic concurrency control. (即写 - 写冲突),答案在数据库应用程序中是众所周知的:使用乐观并发控制。

For a detailed discussion of how to use Optimistic Concurrency with C# and SQL, see Optimistic Concurrency (ADO.NET) . 有关如何在C#和SQL中使用Optimistic Concurrency的详细讨论,请参阅乐观并发(ADO.NET)

I calculate running totals, store them in the same row, and use a constraint to make sure that running totals are non-negative. 我计算运行总计,将它们存储在同一行,并使用约束来确保运行总计是非负数。 Described here: Denormalizing to enforce business rules: Running Totals 这里描述: 非规范化以强制执行业务规则:运行总计

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

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