Transaction.php 17 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
<?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
19
 * <http://www.phpdoctrine.org>.
20
 */
21
Doctrine::autoload('Doctrine_Connection_Module');
22
/**
zYne's avatar
zYne committed
23 24
 * Doctrine_Transaction
 * Handles transaction savepoint and isolation abstraction
25 26
 *
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
zYne's avatar
zYne committed
27
 * @author      Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library)
28 29
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @package     Doctrine
30
 * @subpackage  Transaction
31
 * @link        www.phpdoctrine.org
32 33 34
 * @since       1.0
 * @version     $Revision$
 */
lsmith's avatar
lsmith committed
35 36
class Doctrine_Transaction extends Doctrine_Connection_Module
{
37
    /**
romanb's avatar
romanb committed
38
     * A transaction is in sleep state when it is not active.
39
     */
40
    const STATE_SLEEP       = 0;
41

42
    /**
romanb's avatar
romanb committed
43
     * A transaction is in active state when it is active.
44
     */
45
    const STATE_ACTIVE      = 1;
46

47
    /**
romanb's avatar
romanb committed
48
     * A transaction is in busy state when it is active and has a nesting level > 1.
49
     */
50
    const STATE_BUSY        = 2;
51

52
    /**
53 54 55
     * @var integer $_nestingLevel      The current nesting level of this transaction.
     *                                  A nesting level of 0 means there is currently no active
     *                                  transaction.
56
     */
57 58 59 60 61 62 63 64 65
    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.
romanb's avatar
romanb committed
66
     * @todo package:orm. I guess the DBAL does not start transactions on its own?
67 68
     */
    protected $_internalNestingLevel = 0;
69

70 71
    /**
     * @var array $invalid                  an array containing all invalid records within this transaction
72
     * @todo What about a more verbose name? $invalidRecords?
romanb's avatar
romanb committed
73
     *       package:orm
74
     */
romanb's avatar
romanb committed
75
    protected $invalid = array();
76

zYne's avatar
zYne committed
77 78 79
    /**
     * @var array $savepoints               an array containing all savepoints
     */
romanb's avatar
romanb committed
80
    protected $savePoints = array();
81

82 83
    /**
     * @var array $_collections             an array of Doctrine_Collection objects that were affected during the Transaction
romanb's avatar
romanb committed
84
     * @todo package:orm
85
     */
romanb's avatar
romanb committed
86
    protected $_collections = array();
zYne's avatar
zYne committed
87

zYne's avatar
zYne committed
88
    /**
89
     * addCollection
zYne's avatar
zYne committed
90 91 92 93 94
     * 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
95
     *
zYne's avatar
zYne committed
96
     * @param Doctrine_Collection $coll     a collection to be added
zYne's avatar
zYne committed
97
     * @return Doctrine_Transaction         this object
romanb's avatar
romanb committed
98
     * @todo package:orm
99 100 101 102
     */
    public function addCollection(Doctrine_Collection $coll)
    {
        $this->_collections[] = $coll;
zYne's avatar
zYne committed
103 104 105

        return $this;
    }
106

107 108
    /**
     * getState
109
     * returns the state of this transaction module.
110 111 112 113
     *
     * @see Doctrine_Connection_Transaction::STATE_* constants
     * @return integer          the connection state
     */
lsmith's avatar
lsmith committed
114 115
    public function getState()
    {
116
        switch ($this->_nestingLevel) {
117 118 119 120 121 122 123 124
            case 0:
                return Doctrine_Transaction::STATE_SLEEP;
                break;
            case 1:
                return Doctrine_Transaction::STATE_ACTIVE;
                break;
            default:
                return Doctrine_Transaction::STATE_BUSY;
125 126
        }
    }
zYne's avatar
zYne committed
127

128 129 130 131 132
    /**
     * addInvalid
     * adds record into invalid records list
     *
     * @param Doctrine_Record $record
lsmith's avatar
lsmith committed
133
     * @return boolean        false if record already existed in invalid records list,
134
     *                        otherwise true
romanb's avatar
romanb committed
135
     * @todo package:orm
136
     */
lsmith's avatar
lsmith committed
137 138
    public function addInvalid(Doctrine_Record $record)
    {
139 140
        if (in_array($record, $this->invalid, true)) {
            return false;
lsmith's avatar
lsmith committed
141
        }
142 143 144 145
        $this->invalid[] = $record;
        return true;
    }

146 147 148 149 150

   /**
    * Return the invalid records
    *
    * @return array An array of invalid records
romanb's avatar
romanb committed
151
    * @todo package:orm
152
    */ 
153 154
    public function getInvalid()
    {
155 156 157
        return $this->invalid;
    }

158 159 160 161 162
    /**
     * getTransactionLevel
     * get the current transaction nesting level
     *
     * @return integer
romanb's avatar
romanb committed
163
     * @todo Name suggestion: getNestingLevel(). $transaction->getTransactionLevel() looks odd.
164
     */
lsmith's avatar
lsmith committed
165 166
    public function getTransactionLevel()
    {
167
        return $this->_nestingLevel;
zYne's avatar
zYne committed
168
    }
169 170 171 172 173 174
    
    /**
     * getInternalTransactionLevel
     * get the current internal transaction nesting level
     *
     * @return integer
romanb's avatar
romanb committed
175
     * @todo package:orm. I guess the DBAL does not start transactions itself?
176 177 178 179 180
     */
    public function getInternalTransactionLevel()
    {
        return $this->_internalNestingLevel;
    }
181

182 183 184 185
    /**
     * beginTransaction
     * Start a transaction or set a savepoint.
     *
lsmith's avatar
lsmith committed
186
     * if trying to set a savepoint and there is no active transaction
zYne's avatar
zYne committed
187 188
     * a new transaction is being started
     *
189 190 191
     * This method should only be used by userland-code to initiate transactions.
     * To initiate a transaction from inside Doctrine use {@link beginInternalTransaction()}.
     *
192 193
     * Listeners: onPreTransactionBegin, onTransactionBegin
     *
194
     * @param string $savepoint                 name of a savepoint to set
195
     * @throws Doctrine_Transaction_Exception   if the transaction fails at database level     
196
     * @return integer                          current transaction nesting level
romanb's avatar
romanb committed
197
     * @todo Name suggestion: begin(). $transaction->beginTransaction() looks odd.
198
     */
lsmith's avatar
lsmith committed
199 200
    public function beginTransaction($savepoint = null)
    {
201
        $this->conn->connect();
zYne's avatar
zYne committed
202 203
        
        $listener = $this->conn->getAttribute(Doctrine::ATTR_LISTENER);
204

zYne's avatar
zYne committed
205
        if ( ! is_null($savepoint)) {
zYne's avatar
zYne committed
206
            $this->savePoints[] = $savepoint;
207

zYne's avatar
zYne committed
208 209 210 211 212 213 214 215 216
            $event = new Doctrine_Event($this, Doctrine_Event::SAVEPOINT_CREATE);

            $listener->preSavepointCreate($event);

            if ( ! $event->skipOperation) {
                $this->createSavePoint($savepoint);
            }

            $listener->postSavepointCreate($event);
217
        } else {
218
            if ($this->_nestingLevel == 0) {
219
                $event = new Doctrine_Event($this, Doctrine_Event::TX_BEGIN);
220

zYne's avatar
zYne committed
221
                $listener->preTransactionBegin($event);
222

zYne's avatar
zYne committed
223 224 225
                if ( ! $event->skipOperation) {
                    try {
                        $this->conn->getDbh()->beginTransaction();
226
                    } catch (Exception $e) {
zYne's avatar
zYne committed
227 228
                        throw new Doctrine_Transaction_Exception($e->getMessage());
                    }
229
                }
zYne's avatar
zYne committed
230
                $listener->postTransactionBegin($event);
231
            }
232 233
        }

234
        $level = ++$this->_nestingLevel;
235 236 237

        return $level;
    }
238

239
    /**
zYne's avatar
zYne committed
240
     * commit
241 242
     * Commit the database changes done during a transaction that is in
     * progress or release a savepoint. This function may only be called when
lsmith's avatar
lsmith committed
243
     * auto-committing is disabled, otherwise it will fail.
244
     *
245
     * Listeners: preTransactionCommit, postTransactionCommit
246 247
     *
     * @param string $savepoint                 name of a savepoint to release
248
     * @throws Doctrine_Transaction_Exception   if the transaction fails at database level
249
     * @throws Doctrine_Validator_Exception     if the transaction fails due to record validations
zYne's avatar
zYne committed
250
     * @return boolean                          false if commit couldn't be performed, true otherwise
251
     */
lsmith's avatar
lsmith committed
252 253
    public function commit($savepoint = null)
    {
254 255
        if ($this->_nestingLevel == 0) {
            throw new Doctrine_Transaction_Exception("Commit failed. There is no active transaction.");
256
        }
257 258
        
        $this->conn->connect();
zYne's avatar
zYne committed
259

zYne's avatar
zYne committed
260 261
        $listener = $this->conn->getAttribute(Doctrine::ATTR_LISTENER);

262
        if ( ! is_null($savepoint)) {
263
            $this->_nestingLevel -= $this->removeSavePoints($savepoint);
zYne's avatar
zYne committed
264

zYne's avatar
zYne committed
265 266 267 268 269 270 271 272 273
            $event = new Doctrine_Event($this, Doctrine_Event::SAVEPOINT_COMMIT);

            $listener->preSavepointCommit($event);

            if ( ! $event->skipOperation) {
                $this->releaseSavePoint($savepoint);
            }

            $listener->postSavepointCommit($event);
274 275 276
        } else {
                 
            if ($this->_nestingLevel == 1 || $this->_internalNestingLevel == 1) {
277
                if ( ! empty($this->invalid)) {
278 279 280 281 282
                    if ($this->_internalNestingLevel == 1) {
                        $tmp = $this->invalid;
                        $this->invalid = array();
                        throw new Doctrine_Validator_Exception($tmp);
                    }
283
                }
284 285 286 287 288 289 290 291
                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);
292

293 294 295 296 297 298
                    $listener->preTransactionCommit($event);
                    if ( ! $event->skipOperation) {
                        $this->conn->getDbh()->commit();
                    }
                    $listener->postTransactionCommit($event);
                }
299
            }
zYne's avatar
zYne committed
300
            
301 302 303
            if ($this->_nestingLevel > 0) {
                $this->_nestingLevel--;
            }            
romanb's avatar
romanb committed
304
            if ($this->_internalNestingLevel > 0) {      
305 306
                $this->_internalNestingLevel--;
            } 
307
        }
lsmith's avatar
lsmith committed
308

zYne's avatar
zYne committed
309
        return true;
310
    }
zYne's avatar
zYne committed
311

312 313 314 315 316 317 318
    /**
     * 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.
     *
319
     * this method can be listened with onPreTransactionRollback and onTransactionRollback
320 321
     * eventlistener methods
     *
322 323
     * @param string $savepoint                 name of a savepoint to rollback to   
     * @throws Doctrine_Transaction_Exception   if the rollback operation fails at database level
zYne's avatar
zYne committed
324
     * @return boolean                          false if rollback couldn't be performed, true otherwise
325
     */
lsmith's avatar
lsmith committed
326 327
    public function rollback($savepoint = null)
    {
328 329 330
        if ($this->_nestingLevel == 0) {
            throw new Doctrine_Transaction_Exception("Rollback failed. There is no active transaction.");
        }
331
        
332 333 334 335 336
        $this->conn->connect();

        if ($this->_internalNestingLevel > 1 || $this->_nestingLevel > 1) {
            $this->_internalNestingLevel--;
            $this->_nestingLevel--;
zYne's avatar
zYne committed
337
            return false;
338
        }
339

zYne's avatar
zYne committed
340 341
        $listener = $this->conn->getAttribute(Doctrine::ATTR_LISTENER);

lsmith's avatar
lsmith committed
342
        if ( ! is_null($savepoint)) {
343
            $this->_nestingLevel -= $this->removeSavePoints($savepoint);
zYne's avatar
zYne committed
344 345 346 347 348 349
            $event = new Doctrine_Event($this, Doctrine_Event::SAVEPOINT_ROLLBACK);
            $listener->preSavepointRollback($event);
            if ( ! $event->skipOperation) {
                $this->rollbackSavePoint($savepoint);
            }
            $listener->postSavepointRollback($event);
350
        } else {
351
            $event = new Doctrine_Event($this, Doctrine_Event::TX_ROLLBACK);
zYne's avatar
zYne committed
352
            $listener->preTransactionRollback($event);
353 354
            
            if ( ! $event->skipOperation) {
355 356
                $this->_nestingLevel = 0;
                $this->_internalNestingLevel = 0;
357 358 359 360 361
                try {
                    $this->conn->getDbh()->rollback();
                } catch (Exception $e) {
                    throw new Doctrine_Transaction_Exception($e->getMessage());
                }
362
            }
zYne's avatar
zYne committed
363
            $listener->postTransactionRollback($event);
364
        }
lsmith's avatar
lsmith committed
365

zYne's avatar
zYne committed
366
        return true;
367
    }
zYne's avatar
zYne committed
368

369 370 371 372 373 374 375
    /**
     * releaseSavePoint
     * creates a new savepoint
     *
     * @param string $savepoint     name of a savepoint to create
     * @return void
     */
lsmith's avatar
lsmith committed
376 377
    protected function createSavePoint($savepoint)
    {
378 379
        throw new Doctrine_Transaction_Exception('Savepoints not supported by this driver.');
    }
zYne's avatar
zYne committed
380

381 382 383 384 385 386 387
    /**
     * releaseSavePoint
     * releases given savepoint
     *
     * @param string $savepoint     name of a savepoint to release
     * @return void
     */
lsmith's avatar
lsmith committed
388 389
    protected function releaseSavePoint($savepoint)
    {
390 391
        throw new Doctrine_Transaction_Exception('Savepoints not supported by this driver.');
    }
zYne's avatar
zYne committed
392

393 394 395 396 397 398 399
    /**
     * rollbackSavePoint
     * releases given savepoint
     *
     * @param string $savepoint     name of a savepoint to rollback to
     * @return void
     */
lsmith's avatar
lsmith committed
400 401
    protected function rollbackSavePoint($savepoint)
    {
402 403
        throw new Doctrine_Transaction_Exception('Savepoints not supported by this driver.');
    }
zYne's avatar
zYne committed
404

zYne's avatar
zYne committed
405 406 407 408 409 410
    /**
     * 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
zYne's avatar
zYne committed
411
     * @return integer              removed savepoints
zYne's avatar
zYne committed
412
     */
lsmith's avatar
lsmith committed
413 414
    private function removeSavePoints($savepoint)
    {
415
        $this->savePoints = array_values($this->savePoints);
zYne's avatar
zYne committed
416

zYne's avatar
zYne committed
417 418
        $found = false;
        $i = 0;
zYne's avatar
zYne committed
419

zYne's avatar
zYne committed
420 421 422 423 424 425 426 427 428 429
        foreach ($this->savePoints as $key => $sp) {
            if ( ! $found) {
                if ($sp === $savepoint) {
                    $found = true;
                }
            }
            if ($found) {
                $i++;
                unset($this->savePoints[$key]);
            }
zYne's avatar
zYne committed
430
        }
zYne's avatar
zYne committed
431 432

        return $i;
zYne's avatar
zYne committed
433
    }
434

435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
    /**
     * 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)
     *
453
     * @throws Doctrine_Transaction_Exception           if the feature is not supported by the driver
454 455 456
     * @throws PDOException                             if something fails at the PDO level
     * @return void
     */
lsmith's avatar
lsmith committed
457 458
    public function setIsolation($isolation)
    {
459
        throw new Doctrine_Transaction_Exception('Transaction isolation levels not supported by this driver.');
460 461 462 463 464 465 466
    }

    /**
     * getTransactionIsolation
     *
     * fetches the current session transaction isolation level
     *
lsmith's avatar
lsmith committed
467
     * note: some drivers may support setting the transaction isolation level
468
     * but not fetching it
469 470
     *
     * @throws Doctrine_Transaction_Exception           if the feature is not supported by the driver
471 472 473
     * @throws PDOException                             if something fails at the PDO level
     * @return string                                   returns the current session transaction isolation level
     */
lsmith's avatar
lsmith committed
474 475
    public function getIsolation()
    {
476
        throw new Doctrine_Transaction_Exception('Fetching transaction isolation level not supported by this driver.');
477
    }
478 479 480 481 482 483 484 485 486 487 488 489 490
    
    /**
     * 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);
    }
    
491
}