php - Effect of SELECT ... FOR UPDATE for parallel select statements -
(please excuse english in advance since i'm switzerland , english not mother tongue.)
i think have read every related topic here on going explain now, couldn't find satisfying answer following question/situation.
in mysql table (innodb) have many users, identified unique user_id.
i want call external api every user @ every 60 seconds, since api not allow more 1 request per user per minute.
therefore, have column last_api_call int(12) containing php's time().
now via ajax and/or cronjobs, make api call user has been waiting longest api call:
> select * users last_api_call<time()-60seconds order last_api_call asc limit 1 > update users set last_api_call=time() user_id=user_id_from_above_statement
now session1 gets user_id , before session1 updates last_api_call, session2 might same user_id , there violate api rules sending 2 requests same user after another.
after reading through related topics here, come conclusion "select ... update" solves problem:
session1> > select * users last_api_call<time()-60seconds order last_api_call asc limit 1 update
question: happens session2? next record, or "wait" until session1 releases record? should not same record, since "last_api_call-60()" not mach anymore, should use next row in line?!
in other words, row1 locked through "select ... update" "invisible" session2 or wait same row1 unlocked, see same row1 not match main clause anymore , row handled @ all?
Comments
Post a Comment