PHP Class malkusch\lock\mutex\TransactionalMutex

The critical code is executed within a transaction. The DBS will decide which parts of that code need to be locked (if at all). A failing transaction will be replayed.
Author: Markus Malkusch ([email protected])
Inheritance: extends Mutex
Show file Open project: malkusch/lock

Public Methods

Method Description
__construct ( PDO $pdo, integer $timeout = 3 ) Sets the PDO.
synchronized ( callable $code ) : mixed Executes the critical code within a transaction.

Private Methods

Method Description
checkAutocommit ( PDO $pdo ) Checks that the AUTOCOMMIT mode is turned off.
hasPDOException ( Exception $exception ) : boolean Checks if an exception or any of its previous exceptions is a PDOException.
rollBack ( Exception $exception ) Rolls back a transaction.

Method Details

__construct() public method

The PDO object MUST be configured with PDO::ATTR_ERRMODE to throw exceptions on errors. As this implementation spans a transaction over a unit of work, PDO::ATTR_AUTOCOMMIT SHOULD not be enabled.
public __construct ( PDO $pdo, integer $timeout = 3 )
$pdo PDO The PDO.
$timeout integer The timeout in seconds, default is 3.

synchronized() public method

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.
return mixed The return value of the execution block.