Skip to content

Commit

Permalink
feat: allow default transaction attempts to be changed
Browse files Browse the repository at this point in the history
  • Loading branch information
taka-oyama committed Feb 8, 2024
1 parent 675aa8d commit 23d5035
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions src/Concerns/ManagesTransactions.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,23 @@ trait ManagesTransactions
*/
protected ?Transaction $currentTransaction = null;

protected ?int $maxAttempts = null;

/**
* @inheritDoc
* @template T
* @param Closure(static): T $callback
* @param int $attempts
* @param int $attempts -1 is used as a magic number to indicate the default value
* @return T
*/
public function transaction(Closure $callback, $attempts = Database::MAX_RETRIES)
public function transaction(Closure $callback, $attempts = -1)
{
// -1 is used as a magic number to indicate the default value defined in Database::MAX_RETRIES (+1)
// So, we need to resolve the actual value here.
if ($attempts === -1) {
$attempts = $this->getMaxTransactionAttempts();
}

// Since Cloud Spanner does not support nested transactions,
// we use Laravel's transaction management for nested transactions only.
if ($this->transactions > 0) {
Expand Down Expand Up @@ -225,4 +233,22 @@ protected function handleRollbackException(Throwable $e)

throw $e;
}

/**
* @return int
*/
public function getMaxTransactionAttempts(): int
{
return $this->maxAttempts ??= (Database::MAX_RETRIES + 1);
}

/**
* @param int $attempts
* @return $this
*/
public function setMaxTransactionAttempts(int $attempts): static
{
$this->maxAttempts = $attempts;
return $this;
}
}

0 comments on commit 23d5035

Please sign in to comment.