Skip to content

refactor: use Connection::handleRollbackException for rollback #79

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
merged 2 commits into from
Mar 10, 2023
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ Changed
- Exception previously thrown in `Query/Builder` for `sharedLock`, `lockForUpdate`, `insertGetId` was moved to `Query/Grammar`.
- Query/Builder::lock will now throw `BadMethodCallException` if called. Was ignored in previous versions.

Refactored
- Rollback handling has been refactored to better readability.


# v4.7.0 (Not released yet)

Chore
Expand Down
45 changes: 17 additions & 28 deletions src/Concerns/ManagesTransactions.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
use Google\Cloud\Core\Exception\NotFoundException;
use Google\Cloud\Spanner\Database;
use Google\Cloud\Spanner\Transaction;
use Illuminate\Database\QueryException;
use Throwable;

/**
Expand All @@ -38,8 +37,6 @@ trait ManagesTransactions
*/
protected ?Transaction $currentTransaction = null;

protected bool $ignoreSessionNotFoundErrorOnRollback = false;

/**
* @inheritDoc
* @template T
Expand Down Expand Up @@ -74,27 +71,8 @@ public function transaction(Closure $callback, $attempts = Database::MAX_RETRIES

return $result;
} catch (Throwable $e) {
// if session is lost, there is no way to rollback transaction at all,
// so quietly ignore 'session not found' error in rollBack()
// and then abort current transaction and rerun everything again
$savedIgnoreError = $this->ignoreSessionNotFoundErrorOnRollback;
$exceptionToCheck = $e instanceof QueryException ? $e->getPrevious() : $e;

$this->ignoreSessionNotFoundErrorOnRollback =
$exceptionToCheck instanceOf NotFoundException
&& $this->getSessionNotFoundMode() !== self::THROW_EXCEPTION
&& $this->causedBySessionNotFound($exceptionToCheck);

try {
$this->rollBack();
// rethrow NotFoundException instead of QueryException
$eToThrow = $this->ignoreSessionNotFoundErrorOnRollback ? $exceptionToCheck : $e;
// mute phpstan
assert($eToThrow !== null);
throw $eToThrow;
} finally {
$this->ignoreSessionNotFoundErrorOnRollback = $savedIgnoreError;
}
$this->rollBack();
throw $e;
}
}, ['maxRetries' => $attempts - 1]);

Expand Down Expand Up @@ -188,10 +166,6 @@ protected function performRollBack($toLevel)
if ($this->currentTransaction->state() === Transaction::STATE_ACTIVE) {
$this->currentTransaction->rollBack();
}
} catch (NotFoundException $e) {
if (!$this->ignoreSessionNotFoundErrorOnRollback) {
throw $e;
}
} finally {
$this->currentTransaction = null;
}
Expand Down Expand Up @@ -233,4 +207,19 @@ protected function handleTransactionException($e, $currentAttempt, $maxAttempts)

throw $e;
}

/**
* @param Throwable $e
* @return void
*/
protected function handleRollbackException(Throwable $e)
{
if ($e instanceof NotFoundException) {
// Must be reset so that transaction can be retried.
// otherwise, transactions will remain at 1.
$this->transactions = 0;
}

throw $e;
}
}
10 changes: 7 additions & 3 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ protected function withSessionNotFoundHandling(Closure $callback): mixed

try {
return $callback();
} catch (NotFoundException $e) {
} catch (Throwable $e) {
// ensure if this really error with session
if ($this->causedBySessionNotFound($e)) {
$this->disconnect();
Expand All @@ -492,7 +492,7 @@ protected function withSessionNotFoundHandling(Closure $callback): mixed
$this->reconnect();
try {
return $callback();
} catch (NotFoundException $e) {
} catch (Throwable $e) {
if ($handlerMode === self::CLEAR_SESSION_POOL && $this->causedBySessionNotFound($e)) {
$this->disconnect();
// forcefully clearing sessions, might affect parallel processes
Expand All @@ -518,8 +518,12 @@ protected function withSessionNotFoundHandling(Closure $callback): mixed
*/
public function causedBySessionNotFound(Throwable $e): bool
{
if ($e instanceof QueryException) {
$e = $e->getPrevious();
}

return ($e instanceof NotFoundException)
&& strpos($e->getMessage(), self::SESSION_NOT_FOUND_CONDITION) !== false;
&& str_contains($e->getMessage(), self::SESSION_NOT_FOUND_CONDITION);
}

}