Transaction.php 2.4 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

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

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 {
Qiang Xue committed
25 26 27 28
 *	 $connection->createCommand($sql1)->execute();
 *	 $connection->createCommand($sql2)->execute();
 *	 //.... other SQL executions
 *	 $transaction->commit();
Qiang Xue committed
29
 * } catch(Exception $e) {
Qiang Xue committed
30
 *	 $transaction->rollBack();
w  
Qiang Xue committed
31
 * }
w  
Qiang Xue committed
32
 * ~~~
w  
Qiang Xue committed
33 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
{
w  
Qiang Xue committed
39 40
	/**
	 * @var boolean whether this transaction is active. Only an active transaction
Qiang Xue committed
41
	 * can [[commit()]] or [[rollBack()]]. This property is set true when the transaction is started.
w  
Qiang Xue committed
42 43 44 45 46 47
	 */
	public $active;
	/**
	 * @var Connection the database connection that this transaction is associated with.
	 */
	public $connection;
w  
Qiang Xue committed
48 49 50

	/**
	 * Constructor.
w  
Qiang Xue committed
51
	 * @param Connection $connection the connection associated with this transaction
Qiang Xue committed
52
	 * @param array $config name-value pairs that will be used to initialize the object properties
w  
Qiang Xue committed
53
	 * @see Connection::beginTransaction
w  
Qiang Xue committed
54
	 */
Qiang Xue committed
55
	public function __construct($connection, $config = array())
w  
Qiang Xue committed
56
	{
w  
Qiang Xue committed
57 58
		$this->active = true;
		$this->connection = $connection;
Qiang Xue committed
59
		parent::__construct($config);
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
		} else {
Qiang Xue committed
73
			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
		} else {
Qiang Xue committed
88
			throw new Exception('Failed to roll back transaction: transaction was inactive.');
w  
Qiang Xue committed
89 90 91
		}
	}
}