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.
		
		
		
		
			
				
					92 lines
				
				2.4 KiB
			
		
		
			
		
	
	
					92 lines
				
				2.4 KiB
			| 
											14 years ago
										 | <?php
 | ||
|  | /**
 | ||
| 
											14 years ago
										 |  * Transaction class file.
 | ||
| 
											14 years ago
										 |  *
 | ||
|  |  * @link http://www.yiiframework.com/
 | ||
| 
											14 years ago
										 |  * @copyright Copyright © 2008-2012 Yii Software LLC
 | ||
| 
											14 years ago
										 |  * @license http://www.yiiframework.com/license/
 | ||
|  |  */
 | ||
|  | 
 | ||
| 
											13 years ago
										 | namespace yii\db;
 | ||
| 
											14 years ago
										 | 
 | ||
| 
											14 years ago
										 | use yii\db\Exception;
 | ||
|  | 
 | ||
| 
											14 years ago
										 | /**
 | ||
| 
											14 years ago
										 |  * Transaction represents a DB transaction.
 | ||
|  |  *
 | ||
| 
											14 years ago
										 |  * It is usually created by calling [[Connection::beginTransaction()]].
 | ||
| 
											14 years ago
										 |  *
 | ||
| 
											14 years ago
										 |  * The following code is a typical example of using transactions (note that some
 | ||
|  |  * DBMS may not support transactions):
 | ||
| 
											14 years ago
										 |  *
 | ||
| 
											14 years ago
										 |  * ~~~
 | ||
|  |  * $transaction = $connection->beginTransaction();
 | ||
|  |  * try {
 | ||
| 
											14 years ago
										 |  *	 $connection->createCommand($sql1)->execute();
 | ||
|  |  *	 $connection->createCommand($sql2)->execute();
 | ||
|  |  *	 //.... other SQL executions
 | ||
|  |  *	 $transaction->commit();
 | ||
| 
											14 years ago
										 |  * } catch(Exception $e) {
 | ||
| 
											14 years ago
										 |  *	 $transaction->rollBack();
 | ||
| 
											14 years ago
										 |  * }
 | ||
| 
											14 years ago
										 |  * ~~~
 | ||
| 
											14 years ago
										 |  *
 | ||
|  |  * @author Qiang Xue <qiang.xue@gmail.com>
 | ||
| 
											14 years ago
										 |  * @since 2.0
 | ||
| 
											14 years ago
										 |  */
 | ||
| 
											14 years ago
										 | class Transaction extends \yii\base\Object
 | ||
| 
											14 years ago
										 | {
 | ||
| 
											14 years ago
										 | 	/**
 | ||
|  | 	 * @var boolean whether this transaction is active. Only an active transaction
 | ||
| 
											14 years ago
										 | 	 * can [[commit()]] or [[rollBack()]]. This property is set true when the transaction is started.
 | ||
| 
											14 years ago
										 | 	 */
 | ||
|  | 	public $active;
 | ||
|  | 	/**
 | ||
|  | 	 * @var Connection the database connection that this transaction is associated with.
 | ||
|  | 	 */
 | ||
|  | 	public $connection;
 | ||
| 
											14 years ago
										 | 
 | ||
|  | 	/**
 | ||
|  | 	 * Constructor.
 | ||
| 
											14 years ago
										 | 	 * @param Connection $connection the connection associated with this transaction
 | ||
| 
											13 years ago
										 | 	 * @param array $config name-value pairs that will be used to initialize the object properties
 | ||
| 
											14 years ago
										 | 	 * @see Connection::beginTransaction
 | ||
| 
											14 years ago
										 | 	 */
 | ||
| 
											13 years ago
										 | 	public function __construct($connection, $config = array())
 | ||
| 
											14 years ago
										 | 	{
 | ||
| 
											14 years ago
										 | 		$this->active = true;
 | ||
|  | 		$this->connection = $connection;
 | ||
| 
											13 years ago
										 | 		parent::__construct($config);
 | ||
| 
											14 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Commits a transaction.
 | ||
| 
											14 years ago
										 | 	 * @throws Exception if the transaction or the DB connection is not active.
 | ||
| 
											14 years ago
										 | 	 */
 | ||
|  | 	public function commit()
 | ||
|  | 	{
 | ||
| 
											14 years ago
										 | 		if ($this->active && $this->connection->getActive()) {
 | ||
| 
											14 years ago
										 | 			\Yii::trace('Committing transaction', __CLASS__);
 | ||
| 
											14 years ago
										 | 			$this->connection->pdo->commit();
 | ||
|  | 			$this->active = false;
 | ||
| 
											14 years ago
										 | 		} else {
 | ||
| 
											14 years ago
										 | 			throw new Exception('Failed to commit transaction: transaction was inactive.');
 | ||
| 
											14 years ago
										 | 		}
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Rolls back a transaction.
 | ||
| 
											14 years ago
										 | 	 * @throws Exception if the transaction or the DB connection is not active.
 | ||
| 
											14 years ago
										 | 	 */
 | ||
|  | 	public function rollback()
 | ||
|  | 	{
 | ||
| 
											14 years ago
										 | 		if ($this->active && $this->connection->getActive()) {
 | ||
| 
											14 years ago
										 | 			\Yii::trace('Rolling back transaction', __CLASS__);
 | ||
| 
											14 years ago
										 | 			$this->connection->pdo->rollBack();
 | ||
|  | 			$this->active = false;
 | ||
| 
											14 years ago
										 | 		} else {
 | ||
| 
											14 years ago
										 | 			throw new Exception('Failed to roll back transaction: transaction was inactive.');
 | ||
| 
											14 years ago
										 | 		}
 | ||
|  | 	}
 | ||
|  | }
 |