i have an online hotel booking system which allows multiple users to make bookings almost simultaneously.
When a user requests for free rooms, the system performs a query to get the free rooms and then shows it in a some way ...
Now when user clicks on the "book" button, the free rooms are sent to the server to be booked, i am using a very similar query to this (this is simplified):
Insert into Bookings(room_id) select available_room_id from
dbo.GetAvailableRooms(@StartDate,@EndDate) where available_room_id = @ToBeBookedRoomID;
where " GetAvailableRooms " is a UDF.
what i am hoping to do by this query is to test for the latest time if room is available or not, but i am worried if this is exposed to race conditions ? i am thinking about the case that some user has booked that room in the time between the insert and select operations ... would "insert into .. select" guarantees no race conditions ?
Updated: This is the function sql query :
select AllDays.day_date,alloted_room_id as available_room_id,pl.room_number from HotelsContractsAllotments hl
inner join PlansRooms pl on pl.room_id = hl.alloted_room_id
inner join HotelsContracts hc on hc.contract_id = hl.allotment_contract_id
inner join Hotels h on h.hotel_id = hc.contract_hotel_id
inner join AllDays on day_date >= hc.contract_starting_date and day_date <= hc.contract_end_date
left join BookingsRooms br on day_date >= br.booking_starting_date and day_date <= br.booking_end_date and br.booking_room_id = alloted_room_id
where br.booking_room_id is null and day_date >= @StartingDate and day_date <= @EndDate
Thanks.