1
0
mirror of synced 2024-12-15 07:36:03 +03:00
doctrine2/lib/Doctrine/Transaction.php

498 lines
17 KiB
PHP
Raw Normal View History

<?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.org>.
*/
Doctrine::autoload('Doctrine_Connection_Module');
/**
2007-05-16 02:37:04 +04:00
* Doctrine_Transaction
* Handles transaction savepoint and isolation abstraction
*
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
2006-12-03 01:44:53 +03:00
* @author Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library)
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @package Doctrine
* @subpackage Transaction
* @link www.phpdoctrine.com
* @since 1.0
* @version $Revision$
*/
2006-12-29 17:40:47 +03:00
class Doctrine_Transaction extends Doctrine_Connection_Module
{
/**
* Doctrine_Transaction is in sleep state when it has no active transactions
*/
const STATE_SLEEP = 0;
/**
* Doctrine_Transaction is in active state when it has one active transaction
*/
const STATE_ACTIVE = 1;
/**
* Doctrine_Transaction is in busy state when it has multiple active transactions
*/
const STATE_BUSY = 2;
/**
2007-12-12 18:52:12 +03:00
* @var integer $_nestingLevel The current nesting level of this transaction.
* A nesting level of 0 means there is currently no active
* transaction.
*/
2007-12-12 18:52:12 +03:00
protected $_nestingLevel = 0;
/**
* @var integer $_internalNestingLevel The current internal nesting level of this transaction.
* "Internal" means transactions started by Doctrine itself.
* Therefore the internal nesting level is always
* lower or equal to the overall nesting level.
* A level of 0 means there is currently no active
* transaction that was initiated by Doctrine itself.
*/
protected $_internalNestingLevel = 0;
/**
* @var array $invalid an array containing all invalid records within this transaction
* @todo What about a more verbose name? $invalidRecords?
*/
protected $invalid = array();
2006-12-01 02:51:44 +03:00
/**
* @var array $savepoints an array containing all savepoints
*/
2007-04-18 13:48:23 +04:00
protected $savePoints = array();
/**
* @var array $_collections an array of Doctrine_Collection objects that were affected during the Transaction
*/
protected $_collections = array();
2007-06-26 15:19:25 +04:00
2007-05-23 00:52:19 +04:00
/**
* addCollection
2007-05-23 00:52:19 +04:00
* adds a collection in the internal array of collections
*
* at the end of each commit this array is looped over and
* of every collection Doctrine then takes a snapshot in order
* to keep the collections up to date with the database
*
2007-05-23 00:52:19 +04:00
* @param Doctrine_Collection $coll a collection to be added
2007-06-26 15:19:25 +04:00
* @return Doctrine_Transaction this object
*/
public function addCollection(Doctrine_Collection $coll)
{
$this->_collections[] = $coll;
2007-06-26 15:19:25 +04:00
return $this;
}
/**
* getState
2007-12-12 18:52:12 +03:00
* returns the state of this transaction module.
*
* @see Doctrine_Connection_Transaction::STATE_* constants
* @return integer the connection state
*/
2006-12-29 17:40:47 +03:00
public function getState()
{
2007-12-12 18:52:12 +03:00
switch ($this->_nestingLevel) {
case 0:
return Doctrine_Transaction::STATE_SLEEP;
break;
case 1:
return Doctrine_Transaction::STATE_ACTIVE;
break;
default:
return Doctrine_Transaction::STATE_BUSY;
}
}
2007-06-26 15:19:25 +04:00
/**
* addInvalid
* adds record into invalid records list
*
* @param Doctrine_Record $record
2006-12-29 17:01:31 +03:00
* @return boolean false if record already existed in invalid records list,
* otherwise true
*/
2006-12-29 17:40:47 +03:00
public function addInvalid(Doctrine_Record $record)
{
if (in_array($record, $this->invalid, true)) {
return false;
2006-12-29 17:01:31 +03:00
}
$this->invalid[] = $record;
return true;
}
/**
* Return the invalid records
*
* @return array An array of invalid records
*/
2007-12-12 18:52:12 +03:00
public function getInvalid()
{
return $this->invalid;
}
/**
* getTransactionLevel
* get the current transaction nesting level
*
* @return integer
*/
2006-12-29 17:40:47 +03:00
public function getTransactionLevel()
{
2007-12-12 18:52:12 +03:00
return $this->_nestingLevel;
2007-06-25 22:47:36 +04:00
}
/**
* getInternalTransactionLevel
* get the current internal transaction nesting level
*
* @return integer
*/
public function getInternalTransactionLevel()
{
return $this->_internalNestingLevel;
}
/**
* beginTransaction
* Start a transaction or set a savepoint.
*
2006-12-29 17:01:31 +03:00
* if trying to set a savepoint and there is no active transaction
2006-12-01 02:51:44 +03:00
* a new transaction is being started
*
2007-12-12 18:52:12 +03:00
* This method should only be used by userland-code to initiate transactions.
* To initiate a transaction from inside Doctrine use {@link beginInternalTransaction()}.
*
* Listeners: onPreTransactionBegin, onTransactionBegin
*
* @param string $savepoint name of a savepoint to set
* @throws Doctrine_Transaction_Exception if the transaction fails at database level
* @return integer current transaction nesting level
*/
2006-12-29 17:40:47 +03:00
public function beginTransaction($savepoint = null)
{
$this->conn->connect();
2007-06-25 21:51:19 +04:00
$listener = $this->conn->getAttribute(Doctrine::ATTR_LISTENER);
2007-06-25 22:47:36 +04:00
if ( ! is_null($savepoint)) {
2006-12-01 02:51:44 +03:00
$this->savePoints[] = $savepoint;
2007-06-25 21:51:19 +04:00
$event = new Doctrine_Event($this, Doctrine_Event::SAVEPOINT_CREATE);
$listener->preSavepointCreate($event);
if ( ! $event->skipOperation) {
$this->createSavePoint($savepoint);
}
$listener->postSavepointCreate($event);
} else {
2007-12-12 18:52:12 +03:00
if ($this->_nestingLevel == 0) {
2007-06-25 21:24:20 +04:00
$event = new Doctrine_Event($this, Doctrine_Event::TX_BEGIN);
2007-06-25 21:51:19 +04:00
$listener->preTransactionBegin($event);
2007-06-25 21:51:19 +04:00
if ( ! $event->skipOperation) {
try {
$this->conn->getDbh()->beginTransaction();
2007-12-12 18:52:12 +03:00
} catch (Exception $e) {
2007-06-25 21:51:19 +04:00
throw new Doctrine_Transaction_Exception($e->getMessage());
}
}
2007-06-25 21:51:19 +04:00
$listener->postTransactionBegin($event);
}
}
2007-12-12 18:52:12 +03:00
$level = ++$this->_nestingLevel;
return $level;
}
/**
2006-11-13 23:31:38 +03:00
* commit
* Commit the database changes done during a transaction that is in
* progress or release a savepoint. This function may only be called when
2006-12-29 17:01:31 +03:00
* auto-committing is disabled, otherwise it will fail.
*
2007-06-25 14:08:03 +04:00
* Listeners: preTransactionCommit, postTransactionCommit
*
* @param string $savepoint name of a savepoint to release
* @throws Doctrine_Transaction_Exception if the transaction fails at database level
* @throws Doctrine_Validator_Exception if the transaction fails due to record validations
2006-12-01 02:51:44 +03:00
* @return boolean false if commit couldn't be performed, true otherwise
*/
2006-12-29 17:40:47 +03:00
public function commit($savepoint = null)
{
2007-12-12 18:52:12 +03:00
if ($this->_nestingLevel == 0) {
throw new Doctrine_Transaction_Exception("Commit failed. There is no active transaction.");
}
2007-12-12 18:52:12 +03:00
$this->conn->connect();
2006-12-01 02:51:44 +03:00
2007-06-25 21:51:19 +04:00
$listener = $this->conn->getAttribute(Doctrine::ATTR_LISTENER);
if ( ! is_null($savepoint)) {
2007-12-12 18:52:12 +03:00
$this->_nestingLevel -= $this->removeSavePoints($savepoint);
2006-12-01 02:51:44 +03:00
2007-06-25 21:51:19 +04:00
$event = new Doctrine_Event($this, Doctrine_Event::SAVEPOINT_COMMIT);
$listener->preSavepointCommit($event);
if ( ! $event->skipOperation) {
$this->releaseSavePoint($savepoint);
}
$listener->postSavepointCommit($event);
2007-12-12 18:52:12 +03:00
} else {
if ($this->_nestingLevel == 1 || $this->_internalNestingLevel == 1) {
if ( ! empty($this->invalid)) {
2007-12-12 18:52:12 +03:00
if ($this->_internalNestingLevel == 1) {
// transaction was started by doctrine, so we are responsible
// for a rollback
$this->rollback();
$tmp = $this->invalid;
$this->invalid = array();
throw new Doctrine_Validator_Exception($tmp);
}
}
2007-12-12 18:52:12 +03:00
if ($this->_nestingLevel == 1) {
// take snapshots of all collections used within this transaction
foreach ($this->_collections as $coll) {
$coll->takeSnapshot();
}
$this->_collections = array();
$event = new Doctrine_Event($this, Doctrine_Event::TX_COMMIT);
2007-12-12 18:52:12 +03:00
$listener->preTransactionCommit($event);
if ( ! $event->skipOperation) {
$this->conn->getDbh()->commit();
}
$listener->postTransactionCommit($event);
}
}
2007-06-25 22:47:36 +04:00
2007-12-12 18:52:12 +03:00
if ($this->_nestingLevel > 0) {
$this->_nestingLevel--;
}
if ($this->_internalNestingLevel > 0) {
$this->_internalNestingLevel--;
}
}
2006-12-29 17:01:31 +03:00
2006-12-01 02:51:44 +03:00
return true;
}
2007-06-26 15:19:25 +04: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 can be listened with onPreTransactionRollback and onTransactionRollback
* eventlistener methods
*
* @param string $savepoint name of a savepoint to rollback to
* @throws Doctrine_Transaction_Exception if the rollback operation fails at database level
2006-12-01 02:51:44 +03:00
* @return boolean false if rollback couldn't be performed, true otherwise
* @todo Shouldnt this method only commit a rollback if the transactionLevel is 1
* (STATE_ACTIVE)? Explanation: Otherwise a rollback that is triggered from inside doctrine
* in an (emulated) nested transaction would lead to a complete database level
* rollback even though the client code did not yet want to do that.
* In other words: if the user starts a transaction doctrine shouldnt roll it back.
* Doctrine should only roll back transactions started by doctrine. Thoughts?
*/
2006-12-29 17:40:47 +03:00
public function rollback($savepoint = null)
{
2007-12-12 18:52:12 +03:00
if ($this->_nestingLevel == 0) {
throw new Doctrine_Transaction_Exception("Rollback failed. There is no active transaction.");
}
2007-12-12 18:52:12 +03:00
$this->conn->connect();
if ($this->_internalNestingLevel > 1 || $this->_nestingLevel > 1) {
$this->_internalNestingLevel--;
$this->_nestingLevel--;
2006-12-01 02:51:44 +03:00
return false;
}
2007-06-25 21:51:19 +04:00
$listener = $this->conn->getAttribute(Doctrine::ATTR_LISTENER);
2006-12-29 17:01:31 +03:00
if ( ! is_null($savepoint)) {
2007-12-12 18:52:12 +03:00
$this->_nestingLevel -= $this->removeSavePoints($savepoint);
2006-12-01 02:51:44 +03:00
2007-06-25 21:51:19 +04:00
$event = new Doctrine_Event($this, Doctrine_Event::SAVEPOINT_ROLLBACK);
2007-06-25 21:24:20 +04:00
2007-06-25 21:51:19 +04:00
$listener->preSavepointRollback($event);
2007-06-25 21:24:20 +04:00
2007-06-25 21:51:19 +04:00
if ( ! $event->skipOperation) {
$this->rollbackSavePoint($savepoint);
}
$listener->postSavepointRollback($event);
} else {
2007-06-25 21:24:20 +04:00
$event = new Doctrine_Event($this, Doctrine_Event::TX_ROLLBACK);
2007-06-25 14:08:03 +04:00
2007-06-25 21:51:19 +04:00
$listener->preTransactionRollback($event);
2007-06-25 14:08:03 +04:00
if ( ! $event->skipOperation) {
2007-12-12 18:52:12 +03:00
$this->_nestingLevel = 0;
$this->_internalNestingLevel = 0;
2007-06-25 14:08:03 +04:00
try {
$this->conn->getDbh()->rollback();
} catch (Exception $e) {
throw new Doctrine_Transaction_Exception($e->getMessage());
}
}
2007-06-25 21:51:19 +04:00
$listener->postTransactionRollback($event);
}
2006-12-29 17:01:31 +03:00
2006-12-01 02:51:44 +03:00
return true;
}
2007-06-26 15:19:25 +04:00
/**
* releaseSavePoint
* creates a new savepoint
*
* @param string $savepoint name of a savepoint to create
* @return void
*/
2006-12-29 17:40:47 +03:00
protected function createSavePoint($savepoint)
{
throw new Doctrine_Transaction_Exception('Savepoints not supported by this driver.');
}
2007-06-26 15:19:25 +04:00
/**
* releaseSavePoint
* releases given savepoint
*
* @param string $savepoint name of a savepoint to release
* @return void
*/
2006-12-29 17:40:47 +03:00
protected function releaseSavePoint($savepoint)
{
throw new Doctrine_Transaction_Exception('Savepoints not supported by this driver.');
}
2007-06-26 15:19:25 +04:00
/**
* rollbackSavePoint
* releases given savepoint
*
* @param string $savepoint name of a savepoint to rollback to
* @return void
*/
2006-12-29 17:40:47 +03:00
protected function rollbackSavePoint($savepoint)
{
throw new Doctrine_Transaction_Exception('Savepoints not supported by this driver.');
}
2007-06-26 15:19:25 +04:00
2006-12-01 02:51:44 +03:00
/**
* removeSavePoints
* removes a savepoint from the internal savePoints array of this transaction object
* and all its children savepoints
*
* @param sring $savepoint name of the savepoint to remove
2007-06-25 22:47:36 +04:00
* @return integer removed savepoints
2006-12-01 02:51:44 +03:00
*/
2006-12-29 17:40:47 +03:00
private function removeSavePoints($savepoint)
{
$this->savePoints = array_values($this->savePoints);
2006-12-01 02:51:44 +03:00
2007-06-25 22:47:36 +04:00
$found = false;
$i = 0;
2006-12-01 02:51:44 +03:00
2007-06-25 22:47:36 +04:00
foreach ($this->savePoints as $key => $sp) {
if ( ! $found) {
if ($sp === $savepoint) {
$found = true;
}
}
if ($found) {
$i++;
unset($this->savePoints[$key]);
}
2006-12-01 02:51:44 +03:00
}
2007-06-25 22:47:36 +04:00
return $i;
2006-12-01 02:51:44 +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)
*
* @throws Doctrine_Transaction_Exception if the feature is not supported by the driver
* @throws PDOException if something fails at the PDO level
* @return void
*/
2006-12-29 17:40:47 +03:00
public function setIsolation($isolation)
{
throw new Doctrine_Transaction_Exception('Transaction isolation levels not supported by this driver.');
}
/**
* getTransactionIsolation
*
* fetches the current session transaction isolation level
*
2006-12-29 17:01:31 +03:00
* note: some drivers may support setting the transaction isolation level
* but not fetching it
*
* @throws Doctrine_Transaction_Exception if the feature is not supported by the driver
* @throws PDOException if something fails at the PDO level
* @return string returns the current session transaction isolation level
*/
2006-12-29 17:40:47 +03:00
public function getIsolation()
{
throw new Doctrine_Transaction_Exception('Fetching transaction isolation level not supported by this driver.');
}
2007-12-12 18:52:12 +03:00
/**
* Initiates a transaction.
*
* This method must only be used by Doctrine itself to initiate transactions.
* Userland-code must use {@link beginTransaction()}.
*/
public function beginInternalTransaction($savepoint = null)
{
$this->_internalNestingLevel++;
return $this->beginTransaction($savepoint);
}
}