2006-11-13 23:09:14 +03:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This software consists of voluntary contributions made by many individuals
|
|
|
|
* and is licensed under the LGPL. For more information, see
|
|
|
|
* <http://www.phpdoctrine.com>.
|
|
|
|
*/
|
2006-11-16 15:46:23 +03:00
|
|
|
Doctrine::autoload('Doctrine_Connection_Module');
|
2006-11-13 23:09:14 +03:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
|
|
|
* @package Doctrine
|
|
|
|
* @category Object Relational Mapping
|
|
|
|
* @link www.phpdoctrine.com
|
|
|
|
* @since 1.0
|
|
|
|
* @version $Revision$
|
|
|
|
*/
|
2006-11-16 15:46:23 +03:00
|
|
|
class Doctrine_Transaction extends Doctrine_Connection_Module {
|
2006-11-13 23:09:14 +03:00
|
|
|
/**
|
2006-11-14 21:21:36 +03:00
|
|
|
* Doctrine_Transaction is in sleep state when it has no active transactions
|
2006-11-13 23:09:14 +03:00
|
|
|
*/
|
2006-11-14 21:21:36 +03:00
|
|
|
const STATE_SLEEP = 0;
|
2006-11-13 23:09:14 +03:00
|
|
|
/**
|
|
|
|
* Doctrine_Transaction is in active state when it has one active transaction
|
|
|
|
*/
|
2006-11-14 21:21:36 +03:00
|
|
|
const STATE_ACTIVE = 1;
|
2006-11-13 23:09:14 +03:00
|
|
|
/**
|
|
|
|
* Doctrine_Transaction is in busy state when it has multiple active transactions
|
|
|
|
*/
|
2006-11-14 21:21:36 +03:00
|
|
|
const STATE_BUSY = 2;
|
2006-11-13 23:09:14 +03:00
|
|
|
/**
|
|
|
|
* @var integer $transaction_level the nesting level of transactions, used by transaction methods
|
|
|
|
*/
|
|
|
|
protected $transactionLevel = 0;
|
|
|
|
/**
|
|
|
|
* getState
|
|
|
|
* returns the state of this connection
|
|
|
|
*
|
|
|
|
* @see Doctrine_Connection_Transaction::STATE_* constants
|
|
|
|
* @return integer the connection state
|
|
|
|
*/
|
|
|
|
public function getState() {
|
|
|
|
switch($this->transactionLevel) {
|
|
|
|
case 0:
|
|
|
|
return Doctrine_Transaction::STATE_SLEEP;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
return Doctrine_Transaction::STATE_ACTIVE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return Doctrine_Transaction::STATE_BUSY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* getTransactionLevel
|
|
|
|
* get the current transaction nesting level
|
|
|
|
*
|
|
|
|
* @return integer
|
|
|
|
*/
|
|
|
|
public function getTransactionLevel() {
|
|
|
|
return $this->transactionLevel;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* beginTransaction
|
|
|
|
* Start a transaction or set a savepoint.
|
|
|
|
*
|
2006-11-14 21:21:36 +03:00
|
|
|
* Listeners: onPreTransactionBegin, onTransactionBegin
|
|
|
|
*
|
2006-11-13 23:09:14 +03:00
|
|
|
* @param string $savepoint name of a savepoint to set
|
|
|
|
* @throws Doctrine_Transaction_Exception if trying to create a savepoint and there
|
|
|
|
* are no active transactions
|
|
|
|
*
|
|
|
|
* @return integer current transaction nesting level
|
|
|
|
*/
|
|
|
|
public function beginTransaction($savepoint = null) {
|
|
|
|
$this->conn->getAttribute(Doctrine::ATTR_LISTENER)->onPreTransactionBegin($this->conn);
|
|
|
|
|
|
|
|
if( ! is_null($savepoint)) {
|
|
|
|
if($this->transactionLevel == 0)
|
|
|
|
throw new Doctrine_Transaction_Exception('Savepoint cannot be created when changes are auto committed');
|
|
|
|
|
|
|
|
$this->createSavePoint($savepoint);
|
|
|
|
} else {
|
|
|
|
if($this->transactionLevel == 0)
|
|
|
|
$this->conn->getDbh()->beginTransaction();
|
|
|
|
}
|
|
|
|
|
|
|
|
$level = ++$this->transactionLevel;
|
|
|
|
|
|
|
|
$this->conn->getAttribute(Doctrine::ATTR_LISTENER)->onTransactionBegin($this->conn);
|
|
|
|
|
|
|
|
return $level;
|
|
|
|
}
|
|
|
|
/**
|
2006-11-13 23:31:38 +03:00
|
|
|
* commit
|
2006-11-13 23:09:14 +03:00
|
|
|
* Commit the database changes done during a transaction that is in
|
|
|
|
* progress or release a savepoint. This function may only be called when
|
2006-11-14 21:21:36 +03:00
|
|
|
* auto-committing is disabled, otherwise it will fail.
|
|
|
|
*
|
|
|
|
* Listeners: onPreTransactionCommit, onTransactionCommit
|
2006-11-13 23:09:14 +03:00
|
|
|
*
|
|
|
|
* @param string $savepoint name of a savepoint to release
|
|
|
|
* @throws Doctrine_Transaction_Exception if the transaction fails at PDO level
|
|
|
|
* @throws Doctrine_Transaction_Exception if there are no active transactions
|
|
|
|
* @throws Doctrine_Validator_Exception if the transaction fails due to record validations
|
|
|
|
* @return void
|
|
|
|
*/
|
2006-11-13 23:31:38 +03:00
|
|
|
public function commit($savepoint = null) {
|
2006-11-13 23:09:14 +03:00
|
|
|
if($this->transactionLevel == 0)
|
|
|
|
throw new Doctrine_Transaction_Exception('Commit/release savepoint cannot be done. There is no active transaction.');
|
2006-11-13 23:31:38 +03:00
|
|
|
|
2006-11-13 23:09:14 +03:00
|
|
|
if ( ! is_null($savepoint)) {
|
2006-11-13 23:31:38 +03:00
|
|
|
$this->releaseSavePoint($savepoint);
|
2006-11-13 23:09:14 +03:00
|
|
|
} else {
|
2006-11-13 23:31:38 +03:00
|
|
|
$this->transactionLevel--;
|
|
|
|
|
|
|
|
if($this->transactionLevel == 0) {
|
|
|
|
$this->conn->getAttribute(Doctrine::ATTR_LISTENER)->onPreTransactionCommit($this->conn);
|
2006-11-24 01:54:10 +03:00
|
|
|
|
|
|
|
/**
|
2006-11-13 23:31:38 +03:00
|
|
|
try {
|
|
|
|
$this->bulkDelete();
|
2006-11-14 21:21:36 +03:00
|
|
|
|
2006-11-13 23:31:38 +03:00
|
|
|
} catch(Exception $e) {
|
|
|
|
$this->rollback();
|
2006-11-24 01:54:10 +03:00
|
|
|
|
2006-11-13 23:31:38 +03:00
|
|
|
throw new Doctrine_Connection_Transaction_Exception($e->__toString());
|
|
|
|
}
|
2006-11-24 01:54:10 +03:00
|
|
|
*/
|
|
|
|
/**
|
|
|
|
if($tmp = $this->conn->unitOfWork->getInvalid()) {
|
2006-11-13 23:31:38 +03:00
|
|
|
$this->rollback();
|
2006-11-24 01:54:10 +03:00
|
|
|
|
2006-11-13 23:31:38 +03:00
|
|
|
throw new Doctrine_Validator_Exception($tmp);
|
|
|
|
}
|
2006-11-24 01:54:10 +03:00
|
|
|
*/
|
2006-11-13 23:31:38 +03:00
|
|
|
|
|
|
|
$this->conn->getDbh()->commit();
|
|
|
|
|
2006-11-24 01:54:10 +03:00
|
|
|
//$this->conn->unitOfWork->reset();
|
2006-11-13 23:31:38 +03:00
|
|
|
|
|
|
|
$this->conn->getAttribute(Doctrine::ATTR_LISTENER)->onTransactionCommit($this->conn);
|
2006-11-13 23:09:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* rollback
|
|
|
|
* Cancel any database changes done during a transaction or since a specific
|
|
|
|
* savepoint that is in progress. This function may only be called when
|
|
|
|
* auto-committing is disabled, otherwise it will fail. Therefore, a new
|
|
|
|
* transaction is implicitly started after canceling the pending changes.
|
|
|
|
*
|
|
|
|
* this method listens to onPreTransactionRollback and onTransactionRollback
|
|
|
|
* eventlistener methods
|
|
|
|
*
|
|
|
|
* @param string $savepoint name of a savepoint to rollback to
|
|
|
|
* @throws Doctrine_Transaction_Exception if there are no active transactions
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function rollback($savepoint = null) {
|
|
|
|
if($this->transactionLevel == 0)
|
|
|
|
throw new Doctrine_Transaction_Exception('Rollback cannot be done. There is no active transaction.');
|
|
|
|
|
|
|
|
$this->conn->getAttribute(Doctrine::ATTR_LISTENER)->onPreTransactionRollback($this->conn);
|
|
|
|
|
|
|
|
if ( ! is_null($savepoint)) {
|
|
|
|
$this->rollbackSavePoint($savepoint);
|
|
|
|
} else {
|
|
|
|
$this->unitOfWork->reset();
|
|
|
|
|
|
|
|
$this->transactionLevel = 0;
|
|
|
|
|
|
|
|
$this->conn->getDbh()->rollback();
|
|
|
|
}
|
|
|
|
$this->conn->getAttribute(Doctrine::ATTR_LISTENER)->onTransactionRollback($this->conn);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* releaseSavePoint
|
|
|
|
* creates a new savepoint
|
|
|
|
*
|
|
|
|
* @param string $savepoint name of a savepoint to create
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function createSavePoint($savepoint) {
|
|
|
|
throw new Doctrine_Transaction_Exception('Savepoints not supported by this driver.');
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* releaseSavePoint
|
|
|
|
* releases given savepoint
|
|
|
|
*
|
|
|
|
* @param string $savepoint name of a savepoint to release
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function releaseSavePoint($savepoint) {
|
|
|
|
throw new Doctrine_Transaction_Exception('Savepoints not supported by this driver.');
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* rollbackSavePoint
|
|
|
|
* releases given savepoint
|
|
|
|
*
|
|
|
|
* @param string $savepoint name of a savepoint to rollback to
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function rollbackSavePoint($savepoint) {
|
|
|
|
throw new Doctrine_Transaction_Exception('Savepoints not supported by this driver.');
|
|
|
|
}
|
2006-11-14 21:21:36 +03:00
|
|
|
/**
|
|
|
|
* setIsolation
|
|
|
|
*
|
|
|
|
* Set the transacton isolation level.
|
|
|
|
* (implemented by the connection drivers)
|
|
|
|
*
|
|
|
|
* example:
|
|
|
|
*
|
|
|
|
* <code>
|
|
|
|
* $tx->setIsolation('READ UNCOMMITTED');
|
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* @param string standard isolation level
|
|
|
|
* READ UNCOMMITTED (allows dirty reads)
|
|
|
|
* READ COMMITTED (prevents dirty reads)
|
|
|
|
* REPEATABLE READ (prevents nonrepeatable reads)
|
|
|
|
* SERIALIZABLE (prevents phantom reads)
|
|
|
|
*
|
2006-11-24 01:54:10 +03:00
|
|
|
* @throws Doctrine_Transaction_Exception if the feature is not supported by the driver
|
2006-11-14 21:21:36 +03:00
|
|
|
* @throws PDOException if something fails at the PDO level
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setIsolation($isolation) {
|
2006-11-24 01:54:10 +03:00
|
|
|
throw new Doctrine_Transaction_Exception('Transaction isolation levels not supported by this driver.');
|
2006-11-14 21:21:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* getTransactionIsolation
|
|
|
|
*
|
|
|
|
* fetches the current session transaction isolation level
|
|
|
|
*
|
|
|
|
* note: some drivers may support setting the transaction isolation level
|
|
|
|
* but not fetching it
|
2006-11-24 01:54:10 +03:00
|
|
|
*
|
|
|
|
* @throws Doctrine_Transaction_Exception if the feature is not supported by the driver
|
2006-11-14 21:21:36 +03:00
|
|
|
* @throws PDOException if something fails at the PDO level
|
|
|
|
* @return string returns the current session transaction isolation level
|
|
|
|
*/
|
|
|
|
public function getIsolation() {
|
2006-11-24 01:54:10 +03:00
|
|
|
throw new Doctrine_Transaction_Exception('Fetching transaction isolation level not supported by this driver.');
|
2006-11-14 21:21:36 +03:00
|
|
|
}
|
2006-11-13 23:09:14 +03:00
|
|
|
}
|