Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?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
You can’t perform that action at this time.