问题描述
我正在使用PHP和MySQL编写酒店预订软件.我已经完成了很多工作,但是遇到了比赛条件问题.例如,仅剩一间标准房,当两个客人同时选择它时,这两个人都可以使用.我尝试通过在客人单击付款前的确认按钮时再次检查房间来修复它,但这仍然有问题.当有人先单击确认时,我也尝试将房间状态设置为待处理,但是如果客人决定不付款或只是关闭浏览器,我不知道如何将其恢复为可用状态.我在SO中寻找答案,但实际上并没有找到明确的答案.预先感谢
I am writing a hotel booking software using PHP and MySQL. I am pretty much done with it but I ran into a race condition problem. For example there is only one standard room left and when 2 guests both select it it shows available to both of them. I tried fixing it by checking the room again when the guest clicks confirm before payment but that still has issues. I also tried making the room status to pending when whoever clicks the confirm first but I can't figure out how to change it back to available if the guest decides not to pay or just closes the browser. I searched SO for answers but I didn't really find a definitive answer. Thanks in advance
推荐答案
一种解决方案是将两列添加到数据库的表中.一栏是会话ID或用户ID或正在提供会议室的任何用户.第二列是指示该报价何时到期的时间戳.
One solution is to add two columns to a table in the database. One column is the session ID or user ID or whatever of the user that is being offered the room. The second column is a timestamp indicating when that offer will expire.
然后,在您的应用中,仅显示保留列中具有过期时间戳记的房间. (将初始时间戳记设置为0,以使其开始过期.)选择房间后,请再次检查该列.如果那里的时间戳记未到期,则用户会收到抱歉,您太慢了"的消息.否则,将时间戳记放在未来的15分钟之内,然后继续.
Then, in your app, only show rooms that have an expired timestamp in the hold column. (Set the initial timestamp to 0 so that it starts out expired.) When a room is selected, check the column again. If there's an unexpired timestamp there, the user gets a "sorry, you were too slow" message. Otherwise, put a timestamp there for 15 minutes into the future or whatever, and proceed.
您会在旅行网站和购票网站上看到很多这样的信息,上面写着:我们将再为您保留这些座位14分钟.请在此之前完成交易,否则它将被释放. "
You see this on travel sites and ticket-purchasing sites a lot where it says something like "We're holding these seats for you for another 14 minutes. Please complete the transaction by then or it will be released blah blah blah."
这篇关于如何防止在线酒店预订中出现种族状况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!