Created
December 10, 2014 10:51
-
-
Save cocoiti/45e336db353cd0b77fe8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace XaTestBundle\Doctrine; | |
use XaTestBundle\Doctrine\Connections\TwoPhaseConnection; | |
class TransactionManager | |
{ | |
protected $connections = []; | |
public function __construct($connections = []) | |
{ | |
$this->setConnections($connections); | |
} | |
public function addConnection(TwoPhaseConnection $connection) | |
{ | |
$this->connections[] = $connection; | |
$connection->setTransactionManager($this); | |
} | |
public function clearConnections() | |
{ | |
$this->connections = []; | |
foreach ($this->connections as $connection) { | |
$connection->clearTransactionManager(); | |
} | |
} | |
public function setConnections(array $connections) | |
{ | |
foreach ($connections as $connection) { | |
$this->addConnection($connection); | |
} | |
} | |
public function beginTransaction() | |
{ | |
$successConnections = []; | |
try { | |
foreach ($this->connections as $connection) { | |
$connection->beginTransaction(); | |
$successConnections[] = $connection; | |
} | |
} catch (Exception $e) { | |
foreach ($successsConnections as $connection) { | |
$connection->roleback(); | |
} | |
throw $e; | |
} | |
} | |
public function rollback() | |
{ | |
$successConnections = []; | |
foreach ($this->connections as $connection) { | |
$connection->rollback(); | |
$successConnections[] = $connection; | |
} | |
} | |
public function commit() | |
{ | |
$successConnections = []; | |
try { | |
foreach ($this->connections as $connection) { | |
$connection->prepareTwopahce(); | |
$successConnections[] = $connection; | |
} | |
} catch (Exception $e) { | |
$this->rollback(); | |
} | |
foreach ($this->connections as $connection) { | |
$connection->commit(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment