Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

PHPLIB-373 Use DatabaseTransactionsManager in ManagesTransactions trait #3443

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

Merged
paulinevos merged 1 commit into mongodb:5.5 from paulinevos:373_after-commit
Sep 15, 2025
Merged
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
78 changes: 75 additions & 3 deletions src/Concerns/ManagesTransactions.php
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@
use MongoDB\Client;
use MongoDB\Driver\Exception\RuntimeException;
use MongoDB\Driver\Session;
use MongoDB\Laravel\Connection;
use Throwable;

use function max;
use function MongoDB\with_transaction;
use function property_exists;

/**
* @internal
Expand Down Expand Up @@ -55,32 +58,93 @@ private function getSessionOrThrow(): Session
*/
public function beginTransaction(array $options = []): void
{
$this->runCallbacksBeforeTransaction();

$this->getSessionOrCreate()->startTransaction($options);

$this->handleInitialTransactionState();
}

private function handleInitialTransactionState(): void
{
$this->transactions = 1;

$this->transactionsManager?->begin(
$this->getName(),
$this->transactions,
);

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

/**
* Commit transaction in this session.
*/
public function commit(): void
{
$this->fireConnectionEvent('committing');
$this->getSessionOrThrow()->commitTransaction();
$this->transactions = 0;

$this->handleCommitState();
}

private function handleCommitState(): void
{
[$levelBeingCommitted, $this->transactions] = [
$this->transactions,
max(0, $this->transactions - 1),
];

$this->transactionsManager?->commit(
$this->getName(),
$levelBeingCommitted,
$this->transactions,
);

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

/**
* Abort transaction in this session.
*/
public function rollBack($toLevel = null): void
{
$this->getSessionOrThrow()->abortTransaction();
$session = $this->getSessionOrThrow();
if ($session->isInTransaction()) {
$session->abortTransaction();
}

$this->handleRollbackState();
}

private function handleRollbackState(): void
{
$this->transactions = 0;

$this->transactionsManager?->rollback(
$this->getName(),
$this->transactions,
);

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

private function runCallbacksBeforeTransaction(): void
{
// ToDo: remove conditional once we stop supporting Laravel 10.x
if (property_exists(Connection::class, 'beforeStartingTransaction')) {
foreach ($this->beforeStartingTransaction as $beforeTransactionCallback) {
$beforeTransactionCallback($this);
}
}
}

/**
* Static transaction function realize the with_transaction functionality provided by MongoDB.
*
* @param int $attempts
* @param int $attempts
*
* @throws Throwable
*/
public function transaction(Closure $callback, $attempts = 1, array $options = []): mixed
{
Expand All @@ -93,15 +157,20 @@ public function transaction(Closure $callback, $attempts = 1, array $options = [

if ($attemptsLeft < 0) {
$session->abortTransaction();
$this->handleRollbackState();

return;
}

$this->runCallbacksBeforeTransaction();
$this->handleInitialTransactionState();

// Catch, store, and re-throw any exception thrown during execution
// of the callable. The last exception is re-thrown if the transaction
// was aborted because the number of callback attempts has been exceeded.
try {
$callbackResult = $callback($this);
$this->fireConnectionEvent('committing');
} catch (Throwable $throwable) {
throw $throwable;
}
Expand All @@ -110,9 +179,12 @@ public function transaction(Closure $callback, $attempts = 1, array $options = [
with_transaction($this->getSessionOrCreate(), $callbackFunction, $options);

if ($attemptsLeft < 0 && $throwable) {
$this->handleRollbackState();
throw $throwable;
}

$this->handleCommitState();

return $callbackResult;
}
}
Loading
Loading

AltStyle によって変換されたページ (->オリジナル) /