jeudi 29 décembre 2016

Concurrent issue bypass condition checking

$dailyCap = 50;   

$play = new Play;
$play->user_id = $id;
$play->prize_id = 0;
$play->prize_type = '';
$play->save(); 

DB::beginTransaction();


try
{
   $prize = $this->getPrize();

   if ($prize){
     DB::statement('update plays set prize_id = ?, prize_type = ? where id = ? and (select c from (select count(id) as c from plays where prize_id > 0 and date(created_at) = ?) as iCount) < ?;', [ $prize->id, $prize->type, $play->id,  date('Y-m-d'), $dailyCap ]);

     $play = $play->fresh();
   }

   if (is_numeric($play->prize_id) && $play->prize_id > 0) {
     $prize->user_id = $id;
     $prize->save();
   }
   else {
     $prize = null;
   }

   DB::commit();
}
catch(Exception $e){
   DB::rollback();
   $prize = null;
}

I got 2 table, one is Prizes table, another is Plays table, every time user play the game, i will determine user either get a prize or have no prize. Although i set the daily limit (50) of the prizes to be distribute in each day, it will bypass the daily capping condition due the concurrent request issue. Any hint to solve this?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire