Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 101 additions & 7 deletions system/Database/BaseConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,20 @@ abstract class BaseConnection implements ConnectionInterface
*/
protected bool $transException = false;

/**
* Callbacks to run after the outermost transaction commits.
*
* @var list<callable(): void>
*/
protected array $transCommitCallbacks = [];

/**
* Callbacks to run after the outermost transaction rolls back.
*
* @var list<callable(): void>
*/
protected array $transRollbackCallbacks = [];

Comment thread
memleakd marked this conversation as resolved.
/**
* Array of table aliases.
*
Expand Down Expand Up @@ -985,13 +999,15 @@ public function transComplete(): bool

// The query() function will set this flag to FALSE in the event that a query failed
if ($this->transStatus === false || $this->transFailure === true) {
$this->transRollback();

// If we are NOT running in strict mode, we will reset
// the _trans_status flag so that subsequent groups of
// transactions will be permitted.
if ($this->transStrict === false) {
$this->transStatus = true;
try {
$this->transRollback();
} finally {
// If we are NOT running in strict mode, we will reset
// the _trans_status flag so that subsequent groups of
// transactions will be permitted.
if ($this->transStrict === false) {
$this->transStatus = true;
}
}

return false;
Expand All @@ -1008,6 +1024,48 @@ public function transStatus(): bool
return $this->transStatus;
}

/**
* Register a callback to run after the outermost transaction commits.
*
* If no transaction is active, the callback runs immediately.
*
* @param callable(): void $callback
*
* @return $this
*/
public function afterCommit(callable $callback): static
{
if ($this->transDepth === 0) {
$callback();

return $this;
}

$this->transCommitCallbacks[] = $callback;

return $this;
}

/**
* Register a callback to run after the outermost transaction rolls back.
*
* If no transaction is active, the callback is not run.
*
* @param callable(): void $callback
*
* @return $this
*/
public function afterRollback(callable $callback): static
{
if ($this->transDepth === 0) {
return $this;
}

$this->transRollbackCallbacks[] = $callback;

return $this;
}

/**
* Begin Transaction
*/
Expand Down Expand Up @@ -1055,6 +1113,11 @@ public function transCommit(): bool
if ($this->transDepth > 1 || $this->_transCommit()) {
$this->transDepth--;

if ($this->transDepth === 0) {
$this->transRollbackCallbacks = [];
$this->runTransCommitCallbacks();
}

return true;
}

Expand All @@ -1074,6 +1137,11 @@ public function transRollback(): bool
if ($this->transDepth > 1 || $this->_transRollback()) {
$this->transDepth--;

if ($this->transDepth === 0) {
$this->transCommitCallbacks = [];
$this->runTransRollbackCallbacks();
}

return true;
}

Expand Down Expand Up @@ -1102,6 +1170,32 @@ public function handleTransStatus(): void
}
}

/**
* Run and clear callbacks registered for a successful transaction commit.
*/
protected function runTransCommitCallbacks(): void
{
$callbacks = $this->transCommitCallbacks;
$this->transCommitCallbacks = [];

foreach ($callbacks as $callback) {
$callback();
}
}

/**
* Run and clear callbacks registered for a transaction rollback.
*/
protected function runTransRollbackCallbacks(): void
{
$callbacks = $this->transRollbackCallbacks;
$this->transRollbackCallbacks = [];

foreach ($callbacks as $callback) {
$callback();
}
}

/**
* Begin Transaction
*/
Expand Down
18 changes: 18 additions & 0 deletions system/Database/ConnectionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,24 @@ public function query(string $sql, $binds = null);
*/
public function simpleQuery(string $sql);

/**
* Register a callback to run after the outermost transaction commits.
*
* @param callable(): void $callback
*
* @return $this
*/
public function afterCommit(callable $callback): static;

/**
* Register a callback to run after the outermost transaction rolls back.
*
* @param callable(): void $callback
*
* @return $this
*/
public function afterRollback(callable $callback): static;

/**
* Returns an instance of the query builder for this connection.
*
Expand Down
Loading
Loading