Reputation: 47
I'm making a room reservation system in Web2Py over Google App Engine. When a user is booking a Room the system must be sure that this room is really available and no one else have reserved it just a moment before. To be sure I make a query to see if the room is available, then I make the reservation. The problem is how can I do this transaction in a kind of "Mutual exclusion" to be sure that this room is really for this user?
Thank you!! :)
Upvotes: 0
Views: 196
Reputation: 784
Mutual exclusion is already built into DBMS so we just have to use that. Lets take an example.
First, your table in the model should be defined in such a way that your room number should be unique (use UNIQUE constraint).
When User1 and User2 both query for a room, they should get a response saying the room is vacant. When both the users send the "BOOK" request for that room at the same time, the booking function should directly insert the "BOOK" request of both users into the db. But only one will actually be executed (because of the UNIQUE constraint) and the other will produce a DAL exception. Catch the exception and respond to the user whose "BOOK" request was unsuccesful, saying You just missed this room by an instant :-)
Hope this helped.
Upvotes: 1