Transaction.php 2.82 KB
Newer Older
w  
Qiang Xue committed
1 2 3
<?php
/**
 * @link http://www.yiiframework.com/
Qiang Xue committed
4
 * @copyright Copyright (c) 2008 Yii Software LLC
w  
Qiang Xue committed
5 6 7
 * @license http://www.yiiframework.com/license/
 */

Qiang Xue committed
8
namespace yii\db;
w  
Qiang Xue committed
9

Qiang Xue committed
10
use yii\base\InvalidConfigException;
w  
Qiang Xue committed
11

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

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

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

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

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