Transaction.php 2.32 KB
Newer Older
w  
Qiang Xue committed
1 2
<?php
/**
w  
Qiang Xue committed
3
 * Transaction class file.
w  
Qiang Xue committed
4 5 6
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @link http://www.yiiframework.com/
w  
Qiang Xue committed
7
 * @copyright Copyright &copy; 2008-2012 Yii Software LLC
w  
Qiang Xue committed
8 9 10
 * @license http://www.yiiframework.com/license/
 */

w  
Qiang Xue committed
11 12
namespace yii\db\dao;

w  
Qiang Xue committed
13 14
use yii\db\Exception;

w  
Qiang Xue committed
15
/**
w  
Qiang Xue committed
16 17 18
 * Transaction represents a DB transaction.
 *
 * It is usually created by calling [[Connection::beginTransaction]].
w  
Qiang Xue committed
19
 *
w  
Qiang Xue committed
20 21
 * The following code is a typical example of using transactions (note that some
 * DBMS may not support transactions):
w  
Qiang Xue committed
22
 *
w  
Qiang Xue committed
23 24 25 26 27 28 29
 * ~~~
 * $transaction = $connection->beginTransaction();
 * try {
 *     $connection->createCommand($sql1)->execute();
 *     $connection->createCommand($sql2)->execute();
 *     //.... other SQL executions
 *     $transaction->commit();
w  
Qiang Xue committed
30
 * }
w  
Qiang Xue committed
31 32
 * catch(Exception $e) {
 *     $transaction->rollBack();
w  
Qiang Xue committed
33
 * }
w  
Qiang Xue committed
34
 * ~~~
w  
Qiang Xue committed
35 36
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
w  
Qiang Xue committed
37
 * @since 2.0
w  
Qiang Xue committed
38
 */
Qiang Xue committed
39
class Transaction extends \yii\base\Object
w  
Qiang Xue committed
40
{
w  
Qiang Xue committed
41 42 43 44 45 46 47 48 49
	/**
	 * @var boolean whether this transaction is active. Only an active transaction
	 * can [[commit]] or [[rollBack]]. This property is set true when the transaction is started.
	 */
	public $active;
	/**
	 * @var Connection the database connection that this transaction is associated with.
	 */
	public $connection;
w  
Qiang Xue committed
50 51 52

	/**
	 * Constructor.
w  
Qiang Xue committed
53 54
	 * @param Connection $connection the connection associated with this transaction
	 * @see Connection::beginTransaction
w  
Qiang Xue committed
55
	 */
w  
Qiang Xue committed
56
	public function __construct($connection)
w  
Qiang Xue committed
57
	{
w  
Qiang Xue committed
58 59
		$this->active = true;
		$this->connection = $connection;
w  
Qiang Xue committed
60 61 62 63
	}

	/**
	 * Commits a transaction.
w  
Qiang Xue committed
64
	 * @throws Exception if the transaction or the DB connection is not active.
w  
Qiang Xue committed
65 66 67
	 */
	public function commit()
	{
w  
Qiang Xue committed
68
		if ($this->active && $this->connection->getActive()) {
Qiang Xue committed
69
			\Yii::trace('Committing transaction', __CLASS__);
w  
Qiang Xue committed
70 71
			$this->connection->pdo->commit();
			$this->active = false;
Qiang Xue committed
72 73
		} else {
			throw new Exception('Failed to commit transaction: transaction was inactive.');
w  
Qiang Xue committed
74 75 76 77 78
		}
	}

	/**
	 * Rolls back a transaction.
w  
Qiang Xue committed
79
	 * @throws Exception if the transaction or the DB connection is not active.
w  
Qiang Xue committed
80 81 82
	 */
	public function rollback()
	{
w  
Qiang Xue committed
83
		if ($this->active && $this->connection->getActive()) {
Qiang Xue committed
84
			\Yii::trace('Rolling back transaction', __CLASS__);
w  
Qiang Xue committed
85 86
			$this->connection->pdo->rollBack();
			$this->active = false;
Qiang Xue committed
87 88
		} else {
			throw new Exception('Failed to roll back transaction: transaction was inactive.');
w  
Qiang Xue committed
89 90 91
		}
	}
}