1: <?php
2: /**
3: * CDbTransaction class file
4: *
5: * @author Qiang Xue <qiang.xue@gmail.com>
6: * @link http://www.yiiframework.com/
7: * @copyright 2008-2013 Yii Software LLC
8: * @license http://www.yiiframework.com/license/
9: */
10:
11: /**
12: * CDbTransaction represents a DB transaction.
13: *
14: * It is usually created by calling {@link CDbConnection::beginTransaction}.
15: *
16: * The following code is a common scenario of using transactions:
17: * <pre>
18: * $transaction=$connection->beginTransaction();
19: * try
20: * {
21: * $connection->createCommand($sql1)->execute();
22: * $connection->createCommand($sql2)->execute();
23: * //.... other SQL executions
24: * $transaction->commit();
25: * }
26: * catch(Exception $e)
27: * {
28: * $transaction->rollback();
29: * }
30: * </pre>
31: *
32: * @property CDbConnection $connection The DB connection for this transaction.
33: * @property boolean $active Whether this transaction is active.
34: *
35: * @author Qiang Xue <qiang.xue@gmail.com>
36: * @package system.db
37: * @since 1.0
38: */
39: class CDbTransaction extends CComponent
40: {
41: private $_connection=null;
42: private $_active;
43:
44: /**
45: * Constructor.
46: * @param CDbConnection $connection the connection associated with this transaction
47: * @see CDbConnection::beginTransaction
48: */
49: public function __construct(CDbConnection $connection)
50: {
51: $this->_connection=$connection;
52: $this->_active=true;
53: }
54:
55: /**
56: * Commits a transaction.
57: * @throws CException if the transaction or the DB connection is not active.
58: */
59: public function commit()
60: {
61: if($this->_active && $this->_connection->getActive())
62: {
63: Yii::trace('Committing transaction','system.db.CDbTransaction');
64: $this->_connection->getPdoInstance()->commit();
65: $this->_active=false;
66: }
67: else
68: throw new CDbException(Yii::t('yii','CDbTransaction is inactive and cannot perform commit or roll back operations.'));
69: }
70:
71: /**
72: * Rolls back a transaction.
73: * @throws CException if the transaction or the DB connection is not active.
74: */
75: public function rollback()
76: {
77: if($this->_active && $this->_connection->getActive())
78: {
79: Yii::trace('Rolling back transaction','system.db.CDbTransaction');
80: $this->_connection->getPdoInstance()->rollBack();
81: $this->_active=false;
82: }
83: else
84: throw new CDbException(Yii::t('yii','CDbTransaction is inactive and cannot perform commit or roll back operations.'));
85: }
86:
87: /**
88: * @return CDbConnection the DB connection for this transaction
89: */
90: public function getConnection()
91: {
92: return $this->_connection;
93: }
94:
95: /**
96: * @return boolean whether this transaction is active
97: */
98: public function getActive()
99: {
100: return $this->_active;
101: }
102:
103: /**
104: * @param boolean $value whether this transaction is active
105: */
106: protected function setActive($value)
107: {
108: $this->_active=$value;
109: }
110: }
111: