icecave/reverb

A transaction executor for the Doctrine ORM.

Installs: 85

Dependents: 0

Stars: 3

Watchers: 2

Language: PHP

0.1.0 2014-08-26 09:26 UTC

README

Build Status Test Coverage SemVer

Reverb is a PHP library for executing retry-able transactions with the Doctrine object relational mapper (ORM).

Overview

Reverb provides functionality similar to Doctrine's EntityManager::transactional() method, however there are two major differences in the way that exceptions are handled:

  1. An exception does not cause the entity manager to be closed, instead it is cleared
  2. The transaction may be retried if the type of exception is considered 'retry safe'

An exception is considered 'retry safe' if any of the following conditions are true:

  1. It is an instance of Doctrine's OptimisticLockException or PessimisticLockException
  2. It implements the interface RetrySafeExceptionInterface
  3. A custom retry safe predicate is provided and it returns true when passed the exception

Example

/**
 * 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>')

Contact us