php - Handle limited slots on courses when there are two requests -
i writing page friend people can book courses. courses have limited slots.
on php page free slots of course (from mysql), check if there enough space booking , push data mysql.
the bookings saved in session, when user fills in information , confirms booking code should executed validates , saves of database.
what happens when there 2 requests @ same time? how can guarantee free slots stay free until push data mysql?
i check way
<?php $canbook = true; foreach ($_session["booking"]->courselist $booking) { //for courses in session if ($booking->getcourse()->getfreeslots() < $booking->slots) { //check if there enough slots $canbook = false; break; //not enough free space. cancel } } if ($canbook) { //is data still vaild? (think other requests) $_session["booking"]->save(); //add booking database , reserve slots } else showerror(); //show user error ?>
could problem or never happen? or there better way solve that?
thank you,
tim
if checking of $canbook executed right after validation of free slots should pretty enough regular websites.
if there possibility of concurrent requests (what expected number of active users? how many requests 1 course in 1 second?) can lock record before checking. lets say, add flag in database , proceed validation , record updating if there no flag. if there flag script go sleep second , check again after time. main thread remove flag after saving course , transfer control other threads in way.
Comments
Post a Comment