Page MenuHomePhorge

kolab_sync_transaction_manager.php
No OneTemporary

Authored By
Unknown
Size
5 KB
Referenced Files
None
Subscribers
None

kolab_sync_transaction_manager.php

<?php
/**
* Transaction Manger for Syncroton
*
* This is the central class, all transactions within Syncroton must be handled with.
* For each supported transactionable (backend) this class start a real transaction on
* the first startTransaction request.
*
* Transactions of all transactionable will be commited at once when all requested transactions
* are being commited using this class.
*
* Transactions of all transactionable will be roll back when one rollBack is requested
* using this class.
*/
class kolab_sync_transaction_manager implements Syncroton_TransactionManagerInterface
{
/**
* @var array holds all transactionables with open transactions
*/
protected $_openTransactionables = array();
/**
* @var array list of all open (not commited) transactions
*/
protected $_openTransactions = array();
/**
* @var Syncroton_TransactionManager
*/
private static $_instance = NULL;
/**
* @var Zend_Log
*/
protected $_logger;
/**
* don't clone. Use the singleton.
*/
private function __clone()
{
}
/**
* constructor
*/
private function __construct()
{
if (Syncroton_Registry::isRegistered('loggerBackend')) {
$this->_logger = Syncroton_Registry::get('loggerBackend');
}
}
/**
* @return Tinebase_TransactionManager
*/
public static function getInstance()
{
if (self::$_instance === NULL) {
self::$_instance = new kolab_sync_transaction_manager;
}
return self::$_instance;
}
/**
* starts a transaction
*
* @param mixed $_transactionable
* @return string transactionId
* @throws Tinebase_Exception_UnexpectedValue
*/
public function startTransaction($_transactionable)
{
if ($this->_logger instanceof Zend_Log) {
$this->_logger->debug(__METHOD__ . '::' . __LINE__ . " startTransaction request");
}
if (! in_array($_transactionable, $this->_openTransactionables)) {
if ($this->_logger instanceof Zend_Log) {
$this->_logger->debug(__METHOD__ . '::' . __LINE__ . " new transactionable. Starting transaction on this resource");
}
if ($_transactionable instanceof kolab_sync_db) {
//setAutocommit($_transactionable,false);
$_transactionable->beginTransaction();
}
else {
$this->rollBack();
throw new Syncroton_Exception_UnexpectedValue('Unsupported transactionable!');
}
array_push($this->_openTransactionables, $_transactionable);
}
$transactionId = sha1(mt_rand(). microtime());
array_push($this->_openTransactions, $transactionId);
if ($this->_logger instanceof Zend_Log) {
$this->_logger->debug(__METHOD__ . '::' . __LINE__ . " queued transaction with id $transactionId");
}
return $transactionId;
}
/**
* commits a transaction
*
* @param string $_transactionId
* @return void
*/
public function commitTransaction($_transactionId)
{
if ($this->_logger instanceof Zend_Log) {
$this->_logger->debug(__METHOD__ . '::' . __LINE__ . " commitTransaction request for $_transactionId");
}
$transactionIdx = array_search($_transactionId, $this->_openTransactions);
if ($transactionIdx !== false) {
unset($this->_openTransactions[$transactionIdx]);
}
$numOpenTransactions = count($this->_openTransactions);
if ($numOpenTransactions === 0) {
if ($this->_logger instanceof Zend_Log) {
$this->_logger->debug(__METHOD__ . '::' . __LINE__ . " no more open transactions in queue commiting all transactionables");
}
foreach ($this->_openTransactionables as $transactionableIdx => $transactionable) {
if ($transactionable instanceof kolab_sync_db) {
$transactionable->commit();
//setAutocommit($transactionable,true);
}
}
$this->_openTransactionables = array();
$this->_openTransactions = array();
}
else {
if ($this->_logger instanceof Zend_Log) {
$this->_logger->debug(__METHOD__ . '::' . __LINE__ . " commiting defered, as there are still $numOpenTransactions in the queue");
}
}
}
/**
* perform rollBack on all transactionables with open transactions
*
* @return void
*/
public function rollBack()
{
if ($this->_logger instanceof Zend_Log) {
$this->_logger->debug(__METHOD__ . '::' . __LINE__ . " rollBack request, rollBack all transactionables");
}
foreach ($this->_openTransactionables as $transactionable) {
if ($transactionable instanceof kolab_sync_db) {
$transactionable->rollBack();
//setAutocommit($transactionable,true);
}
}
$this->_openTransactionables = array();
$this->_openTransactions = array();
}
}

File Metadata

Mime Type
text/x-php
Expires
Mon, Apr 6, 12:29 AM (1 w, 7 h ago)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
d0/d1/f2ac5ba10037b5a8dbd71e7c5fa5
Default Alt Text
kolab_sync_transaction_manager.php (5 KB)

Event Timeline