DatabaseTransactionsManager.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. namespace Illuminate\Database;
  3. use Illuminate\Support\Collection;
  4. class DatabaseTransactionsManager
  5. {
  6. /**
  7. * All of the committed transactions.
  8. *
  9. * @var \Illuminate\Support\Collection<int, \Illuminate\Database\DatabaseTransactionRecord>
  10. */
  11. protected $committedTransactions;
  12. /**
  13. * All of the pending transactions.
  14. *
  15. * @var \Illuminate\Support\Collection<int, \Illuminate\Database\DatabaseTransactionRecord>
  16. */
  17. protected $pendingTransactions;
  18. /**
  19. * The current transaction.
  20. *
  21. * @var array
  22. */
  23. protected $currentTransaction = [];
  24. /**
  25. * Create a new database transactions manager instance.
  26. *
  27. * @return void
  28. */
  29. public function __construct()
  30. {
  31. $this->committedTransactions = new Collection;
  32. $this->pendingTransactions = new Collection;
  33. }
  34. /**
  35. * Start a new database transaction.
  36. *
  37. * @param string $connection
  38. * @param int $level
  39. * @return void
  40. */
  41. public function begin($connection, $level)
  42. {
  43. $this->pendingTransactions->push(
  44. $newTransaction = new DatabaseTransactionRecord(
  45. $connection,
  46. $level,
  47. $this->currentTransaction[$connection] ?? null
  48. )
  49. );
  50. $this->currentTransaction[$connection] = $newTransaction;
  51. }
  52. /**
  53. * Commit the root database transaction and execute callbacks.
  54. *
  55. * @param string $connection
  56. * @param int $levelBeingCommitted
  57. * @param int $newTransactionLevel
  58. * @return array
  59. */
  60. public function commit($connection, $levelBeingCommitted, $newTransactionLevel)
  61. {
  62. $this->stageTransactions($connection, $levelBeingCommitted);
  63. if (isset($this->currentTransaction[$connection])) {
  64. $this->currentTransaction[$connection] = $this->currentTransaction[$connection]->parent;
  65. }
  66. if (! $this->afterCommitCallbacksShouldBeExecuted($newTransactionLevel) &&
  67. $newTransactionLevel !== 0) {
  68. return [];
  69. }
  70. // This method is only called when the root database transaction is committed so there
  71. // shouldn't be any pending transactions, but going to clear them here anyways just
  72. // in case. This method could be refactored to receive a level in the future too.
  73. $this->pendingTransactions = $this->pendingTransactions->reject(
  74. fn ($transaction) => $transaction->connection === $connection &&
  75. $transaction->level >= $levelBeingCommitted
  76. )->values();
  77. [$forThisConnection, $forOtherConnections] = $this->committedTransactions->partition(
  78. fn ($transaction) => $transaction->connection == $connection
  79. );
  80. $this->committedTransactions = $forOtherConnections->values();
  81. $forThisConnection->map->executeCallbacks();
  82. return $forThisConnection;
  83. }
  84. /**
  85. * Move relevant pending transactions to a committed state.
  86. *
  87. * @param string $connection
  88. * @param int $levelBeingCommitted
  89. * @return void
  90. */
  91. public function stageTransactions($connection, $levelBeingCommitted)
  92. {
  93. $this->committedTransactions = $this->committedTransactions->merge(
  94. $this->pendingTransactions->filter(
  95. fn ($transaction) => $transaction->connection === $connection &&
  96. $transaction->level >= $levelBeingCommitted
  97. )
  98. );
  99. $this->pendingTransactions = $this->pendingTransactions->reject(
  100. fn ($transaction) => $transaction->connection === $connection &&
  101. $transaction->level >= $levelBeingCommitted
  102. );
  103. }
  104. /**
  105. * Rollback the active database transaction.
  106. *
  107. * @param string $connection
  108. * @param int $newTransactionLevel
  109. * @return void
  110. */
  111. public function rollback($connection, $newTransactionLevel)
  112. {
  113. if ($newTransactionLevel === 0) {
  114. $this->removeAllTransactionsForConnection($connection);
  115. } else {
  116. $this->pendingTransactions = $this->pendingTransactions->reject(
  117. fn ($transaction) => $transaction->connection == $connection &&
  118. $transaction->level > $newTransactionLevel
  119. )->values();
  120. if ($this->currentTransaction) {
  121. do {
  122. $this->removeCommittedTransactionsThatAreChildrenOf($this->currentTransaction[$connection]);
  123. $this->currentTransaction[$connection] = $this->currentTransaction[$connection]->parent;
  124. } while (
  125. isset($this->currentTransaction[$connection]) &&
  126. $this->currentTransaction[$connection]->level > $newTransactionLevel
  127. );
  128. }
  129. }
  130. }
  131. /**
  132. * Remove all pending, completed, and current transactions for the given connection name.
  133. *
  134. * @param string $connection
  135. * @return void
  136. */
  137. protected function removeAllTransactionsForConnection($connection)
  138. {
  139. $this->currentTransaction[$connection] = null;
  140. $this->pendingTransactions = $this->pendingTransactions->reject(
  141. fn ($transaction) => $transaction->connection == $connection
  142. )->values();
  143. $this->committedTransactions = $this->committedTransactions->reject(
  144. fn ($transaction) => $transaction->connection == $connection
  145. )->values();
  146. }
  147. /**
  148. * Remove all transactions that are children of the given transaction.
  149. *
  150. * @param \Illuminate\Database\DatabaseTransactionRecord $transaction
  151. * @return void
  152. */
  153. protected function removeCommittedTransactionsThatAreChildrenOf(DatabaseTransactionRecord $transaction)
  154. {
  155. [$removedTransactions, $this->committedTransactions] = $this->committedTransactions->partition(
  156. fn ($committed) => $committed->connection == $transaction->connection &&
  157. $committed->parent === $transaction
  158. );
  159. // There may be multiple deeply nested transactions that have already committed that we
  160. // also need to remove. We will recurse down the children of all removed transaction
  161. // instances until there are no more deeply nested child transactions for removal.
  162. $removedTransactions->each(
  163. fn ($transaction) => $this->removeCommittedTransactionsThatAreChildrenOf($transaction)
  164. );
  165. }
  166. /**
  167. * Register a transaction callback.
  168. *
  169. * @param callable $callback
  170. * @return void
  171. */
  172. public function addCallback($callback)
  173. {
  174. if ($current = $this->callbackApplicableTransactions()->last()) {
  175. return $current->addCallback($callback);
  176. }
  177. $callback();
  178. }
  179. /**
  180. * Get the transactions that are applicable to callbacks.
  181. *
  182. * @return \Illuminate\Support\Collection<int, \Illuminate\Database\DatabaseTransactionRecord>
  183. */
  184. public function callbackApplicableTransactions()
  185. {
  186. return $this->pendingTransactions;
  187. }
  188. /**
  189. * Determine if after commit callbacks should be executed for the given transaction level.
  190. *
  191. * @param int $level
  192. * @return bool
  193. */
  194. public function afterCommitCallbacksShouldBeExecuted($level)
  195. {
  196. return $level === 0;
  197. }
  198. /**
  199. * Get all of the pending transactions.
  200. *
  201. * @return \Illuminate\Support\Collection
  202. */
  203. public function getPendingTransactions()
  204. {
  205. return $this->pendingTransactions;
  206. }
  207. /**
  208. * Get all of the committed transactions.
  209. *
  210. * @return \Illuminate\Support\Collection
  211. */
  212. public function getCommittedTransactions()
  213. {
  214. return $this->committedTransactions;
  215. }
  216. }