Doctrine 1.2.4
Doctrine_Transaction Class Reference

Inherits Doctrine_Connection_Module.

Inherited by Doctrine_Transaction_Mock, Doctrine_Transaction_Mssql, Doctrine_Transaction_Mysql, Doctrine_Transaction_Oracle, Doctrine_Transaction_Pgsql, and Doctrine_Transaction_Sqlite.

Public Member Functions

 addCollection (Doctrine_Collection $coll)
 
 addInvalid (Doctrine_Record $record)
 
 beginInternalTransaction ($savepoint=null)
 
 beginTransaction ($savepoint=null)
 
 commit ($savepoint=null)
 
 getConnection ()
 
 getInvalid ()
 
 getIsolation ()
 
 getModuleName ()
 
 getState ()
 
 getTransactionLevel ()
 
 rollback ($savepoint=null)
 
 setIsolation ($isolation)
 

Data Fields

const STATE_ACTIVE = 1
 
const STATE_BUSY = 2
 
const STATE_SLEEP = 0
 

Protected Member Functions

 _doBeginTransaction ()
 
 _doCommit ()
 
 _doRollback ()
 
 createSavePoint ($savepoint)
 
 releaseSavePoint ($savepoint)
 
 rollbackSavePoint ($savepoint)
 

Detailed Description

Definition at line 35 of file Transaction.php.

Member Function Documentation

Doctrine_Transaction::_doBeginTransaction ( )
protected

Begins a database transaction.

Definition at line 423 of file Transaction.php.

{
$this->conn->getDbh()->beginTransaction();
}
Doctrine_Transaction::_doCommit ( )
protected

Performs the commit.

Definition at line 415 of file Transaction.php.

{
$this->conn->getDbh()->commit();
}
Doctrine_Transaction::_doRollback ( )
protected

Performs the rollback.

Definition at line 407 of file Transaction.php.

{
$this->conn->getDbh()->rollback();
}
Doctrine_Transaction::addCollection ( Doctrine_Collection  $coll)

addCollection 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

Parameters
Doctrine_Collection$colla collection to be added
Returns
Doctrine_Transaction this object

Definition at line 96 of file Transaction.php.

{
$this->_collections[] = $coll;
return $this;
}
Doctrine_Transaction::addInvalid ( Doctrine_Record  $record)

addInvalid adds record into invalid records list

Parameters
Doctrine_Record$record
Returns
boolean false if record already existed in invalid records list, otherwise true

Definition at line 132 of file Transaction.php.

{
if (in_array($record, $this->invalid, true)) {
return false;
}
$this->invalid[] = $record;
return true;
}
Doctrine_Transaction::beginInternalTransaction (   $savepoint = null)

Initiates a transaction.

This method must only be used by Doctrine itself to initiate transactions. Userland-code must use beginTransaction().

Definition at line 508 of file Transaction.php.

{
$this->_internalNestingLevel++;
return $this->beginTransaction($savepoint);
}
Doctrine_Transaction::beginTransaction (   $savepoint = null)

beginTransaction Start a transaction or set a savepoint.

if trying to set a savepoint and there is no active transaction a new transaction is being started

This method should only be used by userland-code to initiate transactions. To initiate a transaction from inside Doctrine use beginInternalTransaction().

Listeners: onPreTransactionBegin, onTransactionBegin

Parameters
string$savepointname of a savepoint to set
Exceptions
Doctrine_Transaction_Exceptionif the transaction fails at database level
Returns
integer current transaction nesting level

Definition at line 184 of file Transaction.php.

{
$this->conn->connect();
$listener = $this->conn->getAttribute(Doctrine_Core::ATTR_LISTENER);
if ( ! is_null($savepoint)) {
$this->savePoints[] = $savepoint;
$event = new Doctrine_Event($this, Doctrine_Event::SAVEPOINT_CREATE);
$listener->preSavepointCreate($event);
if ( ! $event->skipOperation) {
$this->createSavePoint($savepoint);
}
$listener->postSavepointCreate($event);
} else {
if ($this->_nestingLevel == 0) {
$event = new Doctrine_Event($this, Doctrine_Event::TX_BEGIN);
$listener->preTransactionBegin($event);
if ( ! $event->skipOperation) {
try {
} catch (Exception $e) {
throw new Doctrine_Transaction_Exception($e->getMessage());
}
}
$listener->postTransactionBegin($event);
}
}
$level = ++$this->_nestingLevel;
return $level;
}
Doctrine_Transaction::commit (   $savepoint = null)

Commit the database changes done during a transaction that is in progress or release a savepoint. This function may only be called when auto-committing is disabled, otherwise it will fail.

Listeners: preTransactionCommit, postTransactionCommit

Parameters
string$savepointname of a savepoint to release
Exceptions
Doctrine_Transaction_Exceptionif the transaction fails at database level
Doctrine_Validator_Exceptionif the transaction fails due to record validations
Returns
boolean false if commit couldn't be performed, true otherwise

Definition at line 236 of file Transaction.php.

{
if ($this->_nestingLevel == 0) {
throw new Doctrine_Transaction_Exception("Commit failed. There is no active transaction.");
}
$this->conn->connect();
$listener = $this->conn->getAttribute(Doctrine_Core::ATTR_LISTENER);
if ( ! is_null($savepoint)) {
$this->_nestingLevel -= $this->removeSavePoints($savepoint);
$event = new Doctrine_Event($this, Doctrine_Event::SAVEPOINT_COMMIT);
$listener->preSavepointCommit($event);
if ( ! $event->skipOperation) {
$this->releaseSavePoint($savepoint);
}
$listener->postSavepointCommit($event);
} else {
if ($this->_nestingLevel == 1 || $this->_internalNestingLevel == 1) {
if ( ! empty($this->invalid)) {
if ($this->_internalNestingLevel == 1) {
$tmp = $this->invalid;
$this->invalid = array();
}
}
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);
$listener->preTransactionCommit($event);
if ( ! $event->skipOperation) {
$this->_doCommit();
}
$listener->postTransactionCommit($event);
}
}
if ($this->_nestingLevel > 0) {
$this->_nestingLevel--;
}
if ($this->_internalNestingLevel > 0) {
$this->_internalNestingLevel--;
}
}
return true;
}
Doctrine_Transaction::createSavePoint (   $savepoint)
protected

releaseSavePoint creates a new savepoint

Parameters
string$savepointname of a savepoint to create
Returns
void

Definition at line 375 of file Transaction.php.

{
throw new Doctrine_Transaction_Exception('Savepoints not supported by this driver.');
}
Doctrine_Connection_Module::getConnection ( )
inherited

getConnection returns the connection object this module uses

Returns
Doctrine_Connection

Definition at line 68 of file Module.php.

{
return $this->conn;
}
Doctrine_Transaction::getInvalid ( )

Return the invalid records

Returns
array An array of invalid records

Definition at line 147 of file Transaction.php.

{
return $this->invalid;
}
Doctrine_Transaction::getIsolation ( )

getTransactionIsolation

fetches the current session transaction isolation level

note: some drivers may support setting the transaction isolation level but not fetching it

Exceptions
Doctrine_Transaction_Exceptionif the feature is not supported by the driver
PDOExceptionif something fails at the PDO level
Returns
string returns the current session transaction isolation level

Definition at line 497 of file Transaction.php.

{
throw new Doctrine_Transaction_Exception('Fetching transaction isolation level not supported by this driver.');
}
Doctrine_Connection_Module::getModuleName ( )
inherited

getModuleName returns the name of this module

Returns
string the name of this module

Definition at line 79 of file Module.php.

{
return $this->moduleName;
}
Doctrine_Transaction::getState ( )

getState returns the state of this transaction module.

See Also
Doctrine_Connection_Transaction::STATE_* constants
Returns
integer the connection state

Definition at line 110 of file Transaction.php.

{
switch ($this->_nestingLevel) {
case 0:
break;
case 1:
break;
default:
}
}
Doctrine_Transaction::getTransactionLevel ( )

getTransactionLevel get the current transaction nesting level

Returns
integer

Definition at line 158 of file Transaction.php.

{
return $this->_nestingLevel;
}
Doctrine_Transaction::releaseSavePoint (   $savepoint)
protected

releaseSavePoint releases given savepoint

Parameters
string$savepointname of a savepoint to release
Returns
void

Definition at line 387 of file Transaction.php.

{
throw new Doctrine_Transaction_Exception('Savepoints not supported by this driver.');
}
Doctrine_Transaction::rollback (   $savepoint = null)

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

Parameters
string$savepointname of a savepoint to rollback to
Exceptions
Doctrine_Transaction_Exceptionif the rollback operation fails at database level
Returns
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?

Definition at line 316 of file Transaction.php.

{
if ($this->_nestingLevel == 0) {
throw new Doctrine_Transaction_Exception("Rollback failed. There is no active transaction.");
}
$this->conn->connect();
if ($this->_internalNestingLevel >= 1 && $this->_nestingLevel > 1) {
$this->_internalNestingLevel--;
$this->_nestingLevel--;
return false;
} else if ($this->_nestingLevel > 1) {
$this->_nestingLevel--;
return false;
}
$listener = $this->conn->getAttribute(Doctrine_Core::ATTR_LISTENER);
if ( ! is_null($savepoint)) {
$this->_nestingLevel -= $this->removeSavePoints($savepoint);
$event = new Doctrine_Event($this, Doctrine_Event::SAVEPOINT_ROLLBACK);
$listener->preSavepointRollback($event);
if ( ! $event->skipOperation) {
$this->rollbackSavePoint($savepoint);
}
$listener->postSavepointRollback($event);
} else {
$event = new Doctrine_Event($this, Doctrine_Event::TX_ROLLBACK);
$listener->preTransactionRollback($event);
if ( ! $event->skipOperation) {
$this->_nestingLevel = 0;
$this->_internalNestingLevel = 0;
try {
$this->_doRollback();
} catch (Exception $e) {
throw new Doctrine_Transaction_Exception($e->getMessage());
}
}
$listener->postTransactionRollback($event);
}
return true;
}
Doctrine_Transaction::rollbackSavePoint (   $savepoint)
protected

rollbackSavePoint releases given savepoint

Parameters
string$savepointname of a savepoint to rollback to
Returns
void

Definition at line 399 of file Transaction.php.

{
throw new Doctrine_Transaction_Exception('Savepoints not supported by this driver.');
}
Doctrine_Transaction::setIsolation (   $isolation)

setIsolation

Set the transacton isolation level. (implemented by the connection drivers)

example:

$tx->setIsolation('READ UNCOMMITTED');

Parameters
stringstandard isolation level READ UNCOMMITTED (allows dirty reads) READ COMMITTED (prevents dirty reads) REPEATABLE READ (prevents nonrepeatable reads) SERIALIZABLE (prevents phantom reads)
Exceptions
Doctrine_Transaction_Exceptionif the feature is not supported by the driver
PDOExceptionif something fails at the PDO level
Returns
void

Definition at line 480 of file Transaction.php.

{
throw new Doctrine_Transaction_Exception('Transaction isolation levels not supported by this driver.');
}

Field Documentation

const Doctrine_Transaction::STATE_ACTIVE = 1

Doctrine_Transaction is in active state when it has one active transaction

Definition at line 45 of file Transaction.php.

const Doctrine_Transaction::STATE_BUSY = 2

Doctrine_Transaction is in busy state when it has multiple active transactions

Definition at line 50 of file Transaction.php.

const Doctrine_Transaction::STATE_SLEEP = 0

Doctrine_Transaction is in sleep state when it has no active transactions

Definition at line 40 of file Transaction.php.


The documentation for this class was generated from the following file: