📜  laravel 重试失败的事务 - PHP 代码示例

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

代码示例1
function tryFor10Seconds(Closure $closure) {

  $runTheClosure = function ($closure) {
    try {
      DB::beginTransaction();

      $closure();

      DB::commit();

      return true;
    } catch (Exception $e) {
      DB::rollBack();

      // handle the exception if needed, log it or whatever

      return false;
    }
  };

  $start = time();
  do {
    $result = $runTheClosure($closure);
  } while(!$result && (time() - $start <= 10));

  return $result;
}