My application is storing some data in SQL Table called Transactions. Users are able to sell something from Client1 to Client2 and then Client2 has it. 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). 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). So if 2 users would make run for it it's possible that product from Client1 could be sold multiple times. 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).
So what options do I have? What is your take on this?