I am developing a bidding application in laravel. In this, I have to run cronjob
after each minute and user can also bid from front end. So to avoid collision I am using lockForUpdate()
of laravel. I have placed this in two different functions where bid ids are being processed. And In one place I am using it three times and at other its is being used one time. I have placed my code inside DB::transaction
. But due to some reason I am getting deadlock error whenever same raw is processed by two users at same time.
I am using this as given below:
At One Place
DB::beginTransaction();
try
{
-----Some Code ----
SecondGameBids::where('id', $big)->lockForUpdate()->get();
SecondGameBids::where('id', $big)->update(['final_value' =>0, 'deal_status' => 1]);
-----Some Code ------
SecondGameBids::where('id', $small)->lockForUpdate()->get();
SecondGameBids::where('id', $small)->update(['final_value' =>0, 'deal_status' => 1]);
------Some Code ------
DB::commit();
}
catch (\Exception $e) {
DB::rollback();
}
At Other Place
DB::beginTransaction();
try
{
-----Some Code ----
SecondGameBids::where('id', $big)->lockForUpdate()->get();
SecondGameBids::where('id', $big)->update(['status' => 3]);
------Some Code ------
DB::commit();
}
catch (\Exception $e) {
DB::rollback();
}
Can anyone give me some idea about how to overcome this error?