Transaction.php 2.74 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 the transaction is active. This property is read-only.
 *
w  
Qiang Xue committed
34
 * @author Qiang Xue <qiang.xue@gmail.com>
w  
Qiang Xue committed
35
 * @since 2.0
w  
Qiang Xue committed
36
 */
Qiang Xue committed
37
class Transaction extends \yii\base\Object
w  
Qiang Xue committed
38
{
39 40 41
	/**
	 * @var Connection the database connection that this transaction is associated with.
	 */
Qiang Xue committed
42
	public $db;
w  
Qiang Xue committed
43 44
	/**
	 * @var boolean whether this transaction is active. Only an active transaction
Qiang Xue committed
45
	 * can [[commit()]] or [[rollBack()]]. This property is set true when the transaction is started.
w  
Qiang Xue committed
46
	 */
47 48
	private $_active = false;

w  
Qiang Xue committed
49
	/**
50 51 52
	 * 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
53
	 */
54 55 56 57
	public function getIsActive()
	{
		return $this->_active;
	}
w  
Qiang Xue committed
58 59

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

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

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