Illuminate\Database\Connection::transaction PHP Метод

transaction() публичный Метод

Execute a Closure within a transaction.
public transaction ( Closure $callback, integer $attempts = 1 ) : mixed
$callback Closure
$attempts integer
Результат mixed
    public function transaction(Closure $callback, $attempts = 1)
    {
        for ($a = 1; $a <= $attempts; $a++) {
            $this->beginTransaction();
            // We'll simply execute the given callback within a try / catch block
            // and if we catch any exception we can rollback the transaction
            // so that none of the changes are persisted to the database.
            try {
                $result = $callback($this);
                $this->commit();
            } catch (Exception $e) {
                if ($this->causedByDeadlock($e) && $this->transactions > 1) {
                    --$this->transactions;
                    throw $e;
                }
                $this->rollBack();
                if ($this->causedByDeadlock($e) && $a < $attempts) {
                    continue;
                }
                throw $e;
            } catch (Throwable $e) {
                $this->rollBack();
                throw $e;
            }
            return $result;
        }
    }

Usage Example

Пример #1
0
 /**
  * Enures the given closur is executed within a PDO transaction.
  *
  * @param  Closure  $callback
  * @return void
  */
 public function ensureTransaction(Closure $callback)
 {
     if (!$this->connection->getPdo()->inTransaction()) {
         $this->connection->transaction($callback);
     } else {
         $callback($this->connection);
     }
 }
All Usage Examples Of Illuminate\Database\Connection::transaction