icecave/reverb

该软件包已被废弃,不再维护。未建议替代软件包。

Doctrine ORM 的交易执行器。

0.1.0 2014-08-26 09:26 UTC

This package is not auto-updated.

Last update: 2019-05-28 04:15:41 UTC


README

Build Status Test Coverage SemVer

Reverb 是一个 PHP 库,用于执行带有 Doctrine 对象关系映射器 (ORM) 的可重试事务。

概述

Reverb 提供与 Doctrine 的 EntityManager::transactional() 方法类似的功能,但在异常处理方面有两个主要区别

  1. 异常不会导致实体管理器被 关闭,而是 清除
  2. 如果异常类型被认为是 '重试安全',则可以重试事务

以下任一条件为真时,异常被视为 '重试安全'

  1. 它是 Doctrine 的 OptimisticLockExceptionPessimisticLockException 的实例
  2. 它实现了 RetrySafeExceptionInterface 接口
  3. 提供了一个自定义的重试安全谓词,并且当传递给异常时返回 true

示例

/**
 * This is an example of a custom 'retry safe predicate'.
 *
 * It instructs the transaction executor to retry transactions in the event that
 * MyFancyException is thrown.
 *
 * This same functionality could be accomplished by having MyFancyException
 * implement Icecave\Reverb\RetrySafeException.
 */
$isRetrySafe = function (Exception $e) {
    return $e instanceof MyFancyException;
};

$executor = new Icecave\Reverb\TransactionExecutor($entityManager, $isRetrySafe);

$result = $executor(
    /**
     * This is the transaction body.
     *
     * This function may be called multiple times in order to retry the
     * transaction. Each time it is passed the entity manager along with the
     * number of attempts remaining, not including this one.
     */
    function ($entityManager, $attemptsRemaining) {
        // Perform transactional work with $entityManager

        return '<result>';
    }
);

assert($result === '<result>')

联系我们