📜  悲观锁定 Laravel - 任何代码示例

📅  最后修改于: 2022-03-11 14:56:06.482000             🧑  作者: Mango

代码示例1
exists()) {
      throw new InvalidAccountException();
    }

    $toQuery = Account::whereId($toAccountId);
    if (! $toQuery->exists()) {
      throw new InvalidAccountException();
    }

    $fromAccount = $fromQuery->lockForUpdate()->first();
    if ($amount > $fromAccount->balance) {
      throw new InsufficientBalanceException();
    }

    $toAccount = $toQuery->lockForUpdate()->first();
    $toAccount->balance += $amount;
    $toAccount->save();

    $fromAccount->balance -= $amount;
    $fromAccount->save();

    $transaction = new Transaction();
    $transaction->from_account_id = $fromAccountId;
    $transaction->to_account_id   = $toAccountId;
    $transaction->amount          = $amount;
    $transaction->save();

    DB::commit();
  } catch (\Exception $e) {
    DB::rollBack();
    throw $e;
  }
}