Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] Expand on better transaction manager design #49104

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/Illuminate/Database/Concerns/ManagesTransactions.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

trait ManagesTransactions
{
/** @var \Illuminate\Database\DatabaseTransactionRecord|null */
protected $currentTransaction = null;

/**
* Execute a Closure within a transaction.
*
Expand Down Expand Up @@ -207,7 +210,6 @@ public function commit()
$this->transactionsManager?->commit(
$this->getName(), $levelBeingCommitted, $this->transactions
);

$this->fireConnectionEvent('committed');
}

Expand Down
80 changes: 80 additions & 0 deletions src/Illuminate/Database/DatabaseTransactionRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Illuminate\Database;

use Illuminate\Support\Collection;

class DatabaseTransactionRecord
{
/**
Expand All @@ -25,6 +27,27 @@ class DatabaseTransactionRecord
*/
public $parent;

/**
* The child instances of this transaction.
*
* @var \Illuminate\Support\Collection<int, \Illuminate\Database\DatabaseTransactionRecord[]>
*/
protected $children;

/**
* Whether the transaction has been committed.
*
* @var bool
*/
public $committed = false;

/**
* Whether the transaction has executed its callbacks.
*
* @var bool
*/
public $executedCallbacks = false;

/**
* The callbacks that should be executed after committing.
*
Expand All @@ -45,6 +68,37 @@ public function __construct($connection, $level, ?DatabaseTransactionRecord $par
$this->connection = $connection;
$this->level = $level;
$this->parent = $parent;
$this->children = new Collection();
}

/**
* Get the transaction's children.
*
* @return \Illuminate\Support\Collection<int, \Illuminate\Database\DatabaseTransactionRecord[]>
*/
public function getChildren()
{
return $this->children;
}

public function setParent(DatabaseTransactionRecord $parent)
{
$this->parent = $parent;
}

/**
* @param \Illuminate\Database\DatabaseTransactionRecord $transaction
* @return void
*/
public function addChild($transaction)
{
$transaction->setParent($this);
$this->children[] = $transaction;
}

public function resetChildren()
{
$this->children = new Collection();
}

/**
Expand All @@ -65,9 +119,15 @@ public function addCallback($callback)
*/
public function executeCallbacks()
{
foreach ($this->children as $child) {
$child->executeCallbacks();
}

foreach ($this->callbacks as $callback) {
$callback();
}

$this->executedCallbacks = true;
}

/**
Expand All @@ -79,4 +139,24 @@ public function getCallbacks()
{
return $this->callbacks;
}

public function resetCallbacks()
{
$this->callbacks = [];
}

public function removeChild(DatabaseTransactionRecord $currentTransaction): void
{
$this->children = $this->children->reject(fn ($transaction) => $transaction === $currentTransaction)->values();
}

public function commit()
{
$this->committed = true;
}

public function isRootLevelTransaction()
{
return $this->level === 1;
}
}