0

Laravel Version: 7.15

PHP Version: 7.4.6

Database Driver & Version: MariaDB 10.4.11

Below code is going to return array response for each invoices. Once action method called for id=1 it takes 20 sec to get committed. When the same method called for id=2 response waits until the first transaction is finished. why row-level locking is not working.

protected function actions($id)
    {
      DB::beginTransaction();
      try {
          if($id==1)
            sleep(20);

          $invoice = Invoice::lockForUpdate()->find($id);
          $name = 'Approve';
          if ($invoice->approved) {
             $name = 'Disapprove';      
            }
         } catch (Exception $e) {
            DB::rollBack(); 
         }
      DB::commit();

        return [
            'name' => $name,
            'id' => 'menu-approve',
            'data-status' => $invoice->approved
        ];
    }

It was previously working on Laravel 6.x

A lockForUpdate() locks a specific row until it was committed successfully (Select * from invoices where id=1 FOR UPDATE). But select * from invoices where id=2 FOR UPDATE should not be wait until the first query get committed. Also without specifying a lock ( Select * from invoices) records are not drawn and it waits the first transaction to be committed. What could be the possible reason to lock entire table instead of a single row?

In 7.x when you locked a single row, the entire record(table) gets locked!.

0 Answers0