malkusch\lock\mutex\TransactionalMutex::synchronized PHP Метод

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

It's up to the user to set the correct transaction isolation level. However if the transaction fails, the code will be executed again in a new transaction. Therefore the code must not have any side effects besides SQL statements. Also the isolation level should be conserved for the repeated transaction. A transaction is considered as failed if a PDOException or an exception which has a PDOException as any previous exception was raised. If the code throws any other exception, the transaction is rolled back and won't be replayed.
public synchronized ( callable $code ) : mixed
$code callable The synchronized execution block.
Результат mixed The return value of the execution block.
    public function synchronized(callable $code)
    {
        return $this->loop->execute(function () use($code) {
            try {
                // BEGIN
                $this->pdo->beginTransaction();
            } catch (\PDOException $e) {
                throw new LockAcquireException("Could not begin transaction.", 0, $e);
            }
            try {
                // Unit of work
                $result = call_user_func($code);
                $this->pdo->commit();
                $this->loop->end();
                return $result;
            } catch (\Exception $e) {
                $this->rollBack($e);
                if (self::hasPDOException($e)) {
                    return;
                    // Replay
                } else {
                    throw $e;
                }
            }
        });
    }