Reputation: 896
I'm developping a multiplayer game and I want to connect two players that don't know each other with a research in the database. So I thinked about doing it like this:
My question is: How I can be sure that two players that search a random player in the same moment take two differents players and not the same because SELECT return the same player ? In other words is SELECT synchronous or asynchronous ?
Upvotes: 2
Views: 812
Reputation: 844
I hope you are doing a 3 tier design, and not 2 tier. E.g. does the program connect to a server in the middle which connects to the database (3 tier), or does the program connect directly to the database (2 tier)?
If 2 tier, you may have to wrap the select in some kind of transaction that grabs the player and updates the table so that a second request won't grab the same player. Side note, you may want to consider switching to a 3 Tier design to give yourself better management over what users can and cannot do to your database.
If 3 tier, you can probably avoid this all together by using some kind of locking system provided by the language you are using to build the server in the middle. Most modern programming languages now provide some syntax that allows you to specify that only one thread should run a given piece of code at a time, allowing you to manage these kinds of concurrency issues.
Also consider this question on MySQL concurrency
Upvotes: 1