I want to stop user to make two call at same time. Because if there is two call at same time it create bug in my application and update wrong info. I want it to finish first activity of user and give a error if user try to make two call at same time.
I am recording all user activity in table ->
table feed ->
......................................................
id ! user ! value ! start ! finished ! timestamp
......................................................
7 ! 22 ! 100000 ! ok ! ok ! --------
......................................................
6 ! 22 ! 251632 ! ok ! ok ! --------
......................................................
5 ! 53 ! 125469 ! ok ! ok ! --------
......................................................
4 ! 20 ! 458962 ! ok ! ok ! --------
......................................................
3 ! 19 ! 124587 ! ok ! ok ! --------
......................................................
2 ! 17 ! 321457 ! ok ! ok ! --------
......................................................
I am recording data this way.
Now i am very confused how to check and stop two request at same time of a same user.
I am using php and mysql
thanks
In your database, make a column like "alreadyInUse", mark it as true when a user makes a call and mark it as false, when the user will be done. While handling other users, always check whether alreadyInUse is false or not and if it's false, only then proceed further. I would suggest you to use threads and synchronization in PHP, for getting better results.
You could use a select for update locking read for example:
$mysqli->autocommit(FALSE);
$result = $mysqli->query("SELECT * FROM feed WHERE id = ? FOR UPDATE")->fetch_array();
//do stuff
$mysqli->commit();
or a named lock:
$result = $mysqli->query('SELECT GET_LOCK(?, 5) AS feedlock')->fetch_array();
if($result['feedlock'] != 1)
{
//handle lock error or timeout
return;
}
//do stuff
$result = $mysqli->query('SELECT RELEASE_LOCK(?)')->fetch_array();
I am second on to suggest proceed with the threads and synchronization, but don't think you need an extra column as your start can serve the same purpose and will save you some memory also.