I'm asked to create an online booking system with online payment and I'm wondering what to do in the case when 2 customers booked for the same room(s) at the same time.
For Example:At the same time:
Customer1 and Customer2 booked for a standard room which only has 1 room available. (The Room availability will display that there is still 1 room available). And then they hit the 'confirm' button at the same time.
Hi,
You need to handle this with logic. So when customer 1 and customer 2 are doing booking for same room. Lets say you have one Room entity and you are just updating the status of booking for this. So whenever you update this entity you should call GetForUpdate method of the entity. By this way if any user call this method first will be able to do the booking and for second user it will not available until first don't get finish. Now when it available to second you can again check the status of room booking and can show the appropriate message according to business logic to customer 2.
regards
Yes, a GetForUpdate is in this case the best way to ensure that two or more processes cannot access the same record.
In general, the only other way I know that can prevent double creation is to add a unique index to the entity that you are creating the record in. But in this case, you also want to check for overlap, so a unique index won't cut it.