You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
106 lines
2.7 KiB
106 lines
2.7 KiB
13 years ago
|
<?php
|
||
|
/**
|
||
|
* @link http://www.yiiframework.com/
|
||
12 years ago
|
* @copyright Copyright (c) 2008 Yii Software LLC
|
||
13 years ago
|
* @license http://www.yiiframework.com/license/
|
||
|
*/
|
||
|
|
||
12 years ago
|
namespace yii\db;
|
||
13 years ago
|
|
||
12 years ago
|
use yii\base\InvalidConfigException;
|
||
13 years ago
|
|
||
13 years ago
|
/**
|
||
13 years ago
|
* Transaction represents a DB transaction.
|
||
|
*
|
||
13 years ago
|
* It is usually created by calling [[Connection::beginTransaction()]].
|
||
13 years ago
|
*
|
||
13 years ago
|
* The following code is a typical example of using transactions (note that some
|
||
|
* DBMS may not support transactions):
|
||
13 years ago
|
*
|
||
13 years ago
|
* ~~~
|
||
|
* $transaction = $connection->beginTransaction();
|
||
|
* try {
|
||
12 years ago
|
* $connection->createCommand($sql1)->execute();
|
||
|
* $connection->createCommand($sql2)->execute();
|
||
|
* //.... other SQL executions
|
||
|
* $transaction->commit();
|
||
13 years ago
|
* } catch(Exception $e) {
|
||
12 years ago
|
* $transaction->rollback();
|
||
13 years ago
|
* }
|
||
13 years ago
|
* ~~~
|
||
13 years ago
|
*
|
||
12 years ago
|
* @property boolean $isActive Whether the transaction is active. This property is read-only.
|
||
|
*
|
||
13 years ago
|
* @author Qiang Xue <qiang.xue@gmail.com>
|
||
13 years ago
|
* @since 2.0
|
||
13 years ago
|
*/
|
||
13 years ago
|
class Transaction extends \yii\base\Object
|
||
13 years ago
|
{
|
||
13 years ago
|
/**
|
||
12 years ago
|
* @var Connection the database connection that this transaction is associated with.
|
||
|
*/
|
||
12 years ago
|
public $db;
|
||
12 years ago
|
/**
|
||
13 years ago
|
* @var boolean whether this transaction is active. Only an active transaction
|
||
12 years ago
|
* can [[commit()]] or [[rollback()]]. This property is set true when the transaction is started.
|
||
13 years ago
|
*/
|
||
12 years ago
|
private $_active = false;
|
||
|
|
||
13 years ago
|
/**
|
||
12 years ago
|
* Returns a value indicating whether this transaction is active.
|
||
|
* @return boolean whether this transaction is active. Only an active transaction
|
||
12 years ago
|
* can [[commit()]] or [[rollback()]].
|
||
13 years ago
|
*/
|
||
12 years ago
|
public function getIsActive()
|
||
|
{
|
||
|
return $this->_active;
|
||
|
}
|
||
13 years ago
|
|
||
|
/**
|
||
12 years ago
|
* Begins a transaction.
|
||
12 years ago
|
* @throws InvalidConfigException if [[connection]] is null
|
||
13 years ago
|
*/
|
||
12 years ago
|
public function begin()
|
||
13 years ago
|
{
|
||
12 years ago
|
if (!$this->_active) {
|
||
12 years ago
|
if ($this->db === null) {
|
||
|
throw new InvalidConfigException('Transaction::db must be set.');
|
||
12 years ago
|
}
|
||
12 years ago
|
\Yii::trace('Starting transaction', __METHOD__);
|
||
12 years ago
|
$this->db->open();
|
||
|
$this->db->pdo->beginTransaction();
|
||
12 years ago
|
$this->_active = true;
|
||
|
}
|
||
13 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Commits a transaction.
|
||
13 years ago
|
* @throws Exception if the transaction or the DB connection is not active.
|
||
13 years ago
|
*/
|
||
|
public function commit()
|
||
|
{
|
||
12 years ago
|
if ($this->_active && $this->db && $this->db->isActive) {
|
||
12 years ago
|
\Yii::trace('Committing transaction', __METHOD__);
|
||
12 years ago
|
$this->db->pdo->commit();
|
||
12 years ago
|
$this->_active = false;
|
||
13 years ago
|
} else {
|
||
13 years ago
|
throw new Exception('Failed to commit transaction: transaction was inactive.');
|
||
13 years ago
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Rolls back a transaction.
|
||
13 years ago
|
* @throws Exception if the transaction or the DB connection is not active.
|
||
13 years ago
|
*/
|
||
|
public function rollback()
|
||
|
{
|
||
12 years ago
|
if ($this->_active && $this->db && $this->db->isActive) {
|
||
12 years ago
|
\Yii::trace('Rolling back transaction', __METHOD__);
|
||
12 years ago
|
$this->db->pdo->rollBack();
|
||
12 years ago
|
$this->_active = false;
|
||
13 years ago
|
} else {
|
||
13 years ago
|
throw new Exception('Failed to roll back transaction: transaction was inactive.');
|
||
13 years ago
|
}
|
||
|
}
|
||
|
}
|