Transaction.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\db;
  8. use yii\base\InvalidConfigException;
  9. /**
  10. * Transaction represents a DB transaction.
  11. *
  12. * It is usually created by calling [[Connection::beginTransaction()]].
  13. *
  14. * The following code is a typical example of using transactions (note that some
  15. * DBMS may not support transactions):
  16. *
  17. * ~~~
  18. * $transaction = $connection->beginTransaction();
  19. * try {
  20. * $connection->createCommand($sql1)->execute();
  21. * $connection->createCommand($sql2)->execute();
  22. * //.... other SQL executions
  23. * $transaction->commit();
  24. * } catch(Exception $e) {
  25. * $transaction->rollback();
  26. * }
  27. * ~~~
  28. *
  29. * @property boolean $isActive Whether this transaction is active. Only an active transaction can [[commit()]]
  30. * or [[rollback()]]. This property is read-only.
  31. *
  32. * @author Qiang Xue <[email protected]>
  33. * @since 2.0
  34. */
  35. class Transaction extends \yii\base\Object
  36. {
  37. /**
  38. * @var Connection the database connection that this transaction is associated with.
  39. */
  40. public $db;
  41. /**
  42. * @var boolean whether this transaction is active. Only an active transaction
  43. * can [[commit()]] or [[rollback()]]. This property is set true when the transaction is started.
  44. */
  45. private $_active = false;
  46. /**
  47. * Returns a value indicating whether this transaction is active.
  48. * @return boolean whether this transaction is active. Only an active transaction
  49. * can [[commit()]] or [[rollback()]].
  50. */
  51. public function getIsActive()
  52. {
  53. return $this->_active;
  54. }
  55. /**
  56. * Begins a transaction.
  57. * @throws InvalidConfigException if [[connection]] is null
  58. */
  59. public function begin()
  60. {
  61. if (!$this->_active) {
  62. if ($this->db === null) {
  63. throw new InvalidConfigException('Transaction::db must be set.');
  64. }
  65. \Yii::trace('Starting transaction', __METHOD__);
  66. $this->db->open();
  67. $this->db->pdo->beginTransaction();
  68. $this->_active = true;
  69. }
  70. }
  71. /**
  72. * Commits a transaction.
  73. * @throws Exception if the transaction or the DB connection is not active.
  74. */
  75. public function commit()
  76. {
  77. if ($this->_active && $this->db && $this->db->isActive) {
  78. \Yii::trace('Committing transaction', __METHOD__);
  79. $this->db->pdo->commit();
  80. $this->_active = false;
  81. } else {
  82. throw new Exception('Failed to commit transaction: transaction was inactive.');
  83. }
  84. }
  85. /**
  86. * Rolls back a transaction.
  87. * @throws Exception if the transaction or the DB connection is not active.
  88. */
  89. public function rollback()
  90. {
  91. if ($this->_active && $this->db && $this->db->isActive) {
  92. \Yii::trace('Rolling back transaction', __METHOD__);
  93. $this->db->pdo->rollBack();
  94. $this->_active = false;
  95. } else {
  96. throw new Exception('Failed to roll back transaction: transaction was inactive.');
  97. }
  98. }
  99. }