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

Qiang Xue committed
10
namespace yii\db;
w  
Qiang Xue committed
11

Qiang Xue committed
12
use yii\base\InvalidConfigException;
w  
Qiang Xue committed
13

w  
Qiang Xue committed
14
/**
w  
Qiang Xue committed
15 16
 * Transaction represents a DB transaction.
 *
Qiang Xue committed
17
 * It is usually created by calling [[Connection::beginTransaction()]].
w  
Qiang Xue committed
18
 *
w  
Qiang Xue committed
19 20
 * The following code is a typical example of using transactions (note that some
 * DBMS may not support transactions):
w  
Qiang Xue committed
21
 *
w  
Qiang Xue committed
22 23 24
 * ~~~
 * $transaction = $connection->beginTransaction();
 * try {
25 26 27 28
 *     $connection->createCommand($sql1)->execute();
 *     $connection->createCommand($sql2)->execute();
 *     //.... other SQL executions
 *     $transaction->commit();
Qiang Xue committed
29
 * } catch(Exception $e) {
30
 *     $transaction->rollBack();
w  
Qiang Xue committed
31
 * }
w  
Qiang Xue committed
32
 * ~~~
w  
Qiang Xue committed
33
 *
34 35
 * @property boolean $isActive Whether the transaction is active. This property is read-only.
 *
w  
Qiang Xue committed
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
{
41 42 43
	/**
	 * @var Connection the database connection that this transaction is associated with.
	 */
Qiang Xue committed
44
	public $db;
w  
Qiang Xue committed
45 46
	/**
	 * @var boolean whether this transaction is active. Only an active transaction
Qiang Xue committed
47
	 * can [[commit()]] or [[rollBack()]]. This property is set true when the transaction is started.
w  
Qiang Xue committed
48
	 */
49 50
	private $_active = false;

w  
Qiang Xue committed
51
	/**
52 53 54
	 * Returns a value indicating whether this transaction is active.
	 * @return boolean whether this transaction is active. Only an active transaction
	 * can [[commit()]] or [[rollBack()]].
w  
Qiang Xue committed
55
	 */
56 57 58 59
	public function getIsActive()
	{
		return $this->_active;
	}
w  
Qiang Xue committed
60 61

	/**
62
	 * Begins a transaction.
Qiang Xue committed
63
	 * @throws InvalidConfigException if [[connection]] is null
w  
Qiang Xue committed
64
	 */
65
	public function begin()
w  
Qiang Xue committed
66
	{
67
		if (!$this->_active) {
Qiang Xue committed
68 69
			if ($this->db === null) {
				throw new InvalidConfigException('Transaction::db must be set.');
70 71
			}
			\Yii::trace('Starting transaction', __CLASS__);
Qiang Xue committed
72 73
			$this->db->open();
			$this->db->pdo->beginTransaction();
74 75
			$this->_active = true;
		}
w  
Qiang Xue committed
76 77 78 79
	}

	/**
	 * Commits a transaction.
w  
Qiang Xue committed
80
	 * @throws Exception if the transaction or the DB connection is not active.
w  
Qiang Xue committed
81 82 83
	 */
	public function commit()
	{
Qiang Xue committed
84
		if ($this->_active && $this->db && $this->db->isActive) {
Qiang Xue committed
85
			\Yii::trace('Committing transaction', __CLASS__);
Qiang Xue committed
86
			$this->db->pdo->commit();
87
			$this->_active = false;
Qiang Xue committed
88
		} else {
Qiang Xue committed
89
			throw new Exception('Failed to commit transaction: transaction was inactive.');
w  
Qiang Xue committed
90 91 92 93 94
		}
	}

	/**
	 * Rolls back a transaction.
w  
Qiang Xue committed
95
	 * @throws Exception if the transaction or the DB connection is not active.
w  
Qiang Xue committed
96 97 98
	 */
	public function rollback()
	{
Qiang Xue committed
99
		if ($this->_active && $this->db && $this->db->isActive) {
Qiang Xue committed
100
			\Yii::trace('Rolling back transaction', __CLASS__);
Qiang Xue committed
101
			$this->db->pdo->commit();
102
			$this->_active = false;
Qiang Xue committed
103
		} else {
Qiang Xue committed
104
			throw new Exception('Failed to roll back transaction: transaction was inactive.');
w  
Qiang Xue committed
105 106 107
		}
	}
}