这是否足够,或者我有竞争条件吗?
我正在为网络编写一种策略型多用户游戏。它有一个游戏场(X × Y 方块),我计划将其序列化并存储在 MySQL(innodb)数据库中的 BLOB 中,每个正在进行的游戏一行。
我现在尝试找出一种好方法,使数据库随着游戏场的任何更改而更新,同时找到一种方便的解决方案来处理加载页面和实际之间的时间范围内发生在游戏场上的事情采取行动。
我不使用 AJAX。
每场比赛最多有20名玩家,每个玩家在24小时内走1到10步,所以这是一场“慢”游戏。
我的计划(到目前为止)是在 blob 旁边存储一种游戏场的校验和,并在尝试对游戏场进行更改之前将数据库状态与加载的状态进行比较。
我担心的是如何防止竞争条件。
是否足以:
- 开始交易。
- 如果校验和不同,则从表中加载比赛场
- - 如果校验和未更改,则回滚并更新用户视图
- - 更新表并提交更改
BEGIN TRANSACTION
是否足以阻止比赛,或者我需要在步骤中做更多事情2 显示我更新表格的意图?
感谢所有的建议。
I'm writing a strategy-kind of multi user game for the web. It has a playfield (X by Y squares) that I plan on serialize and store in a BLOB in a MySQL (innodb) database, one row for each ongoing game.
I now try to figure out a good way of keeping the database updated with any changes to the playfield, and at the same time finding a convenient solution to how to handle things that happen to the playfield in the time frame between loading the page and actually making a move.
I don't use AJAX.
There will be at most 20 players in each game, each player making between 1 and 10 moves in 24 hours, so it is a "slow" game.
My plan (so far) is to also store a kind of checksum for the playfield next to the blob and compare the databases state with the state loaded before trying to make changes to the playfield.
What I worry about is how to prevent race conditions.
Is it enough to:
- Begin transaction.
- load playfield from table
- if checksum differs - rollback and update the users view
- if checksum unchanged - update table and commit changes
Is the BEGIN TRANSACTION
enough to block the race, or do I need to do something more in step 2 to show my intent to update the table?
Thankful for all advice.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用 SELECT ... FOR UPDATE 当您从数据库加载游戏场时,它将阻止其他选择,直到您提交或回滚事务。
If you use SELECT ... FOR UPDATE when you load the playfield from the database, it will block other selects until you commit or rollback the transaction.
不可以。您需要对需要防止更新冲突的表发出 LOCK TABLES 命令。这看起来像...
更多详细信息可以在这里找到... http://dev.mysql.com/doc/refman/5.1/en/lock-tables.html
不要忘记之后解锁它们!
No. You will need to issue a LOCK TABLES command for the tables you need to protect against conflicting updates. This would look something like...
More details may be found here... http://dev.mysql.com/doc/refman/5.1/en/lock-tables.html
Don't forget to UNLOCK them afterwards!