CDbTransaction.php 2.7 KB

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