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

fix: Transaction state was not being cleared if rolled back failed #107

Merged
merged 2 commits into from
May 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ Changed
- `Connection::waitForOperation` and `Connection::isDoneOperation` has been removed. (#99)
- Update `export-ignore` entries in `.gitattributes` (#104)

Fixed
- Transaction state was not being cleared if rolled back failed. (#106)
taka-oyama marked this conversation as resolved.
Show resolved Hide resolved

# v5.1.0

Added
Expand Down
8 changes: 3 additions & 5 deletions src/Concerns/ManagesTransactions.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,9 @@ protected function handleTransactionException($e, $currentAttempt, $maxAttempts)
*/
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;
}
// Must be reset so that transaction can be retried.
// otherwise, transactions will remain at 1.
$this->transactions = 0;

throw $e;
}
Expand Down
43 changes: 43 additions & 0 deletions tests/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use Colopl\Spanner\TimestampBound\ReadTimestamp;
use Colopl\Spanner\TimestampBound\StrongRead;
use Google\Auth\FetchAuthTokenInterface;
use Google\Cloud\Core\Exception\AbortedException;
use Google\Cloud\Core\Exception\NotFoundException;
use Google\Cloud\Spanner\KeySet;
use Google\Cloud\Spanner\Session\CacheSessionPool;
Expand All @@ -37,6 +38,8 @@
use Illuminate\Database\Events\TransactionCommitted;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Event;
use LogicException;
use RuntimeException;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use function dirname;
use function fileperms;
Expand Down Expand Up @@ -394,4 +397,44 @@ public function test_Connection_transaction_reset_on_exceptions(): void
self::assertNull($conn->getCurrentTransaction());
self::assertSame(0, $conn->transactionLevel());
}

public function test_connection_transaction_reset_on_rollback_exceptions(): void
{
$base = $this->getDefaultConnection();

$conn = new class($base) extends Connection {
public function __construct(Connection $base)
{
parent::__construct(
$base->instanceId,
$base->database,
$base->tablePrefix,
$base->config,
$base->authCache,
$base->sessionPool,
);
}

protected function performRollBack($toLevel): void
{
$this->currentTransaction = null;
throw new AbortedException('NG');
}
};

try {
$conn->transaction(function (Connection $conn): mixed {
self::assertTrue($conn->inTransaction());
self::assertNotNull($conn->getCurrentTransaction());
self::assertSame(1, $conn->transactionLevel());
throw new RuntimeException('Trigger rollback');
});
} catch(AbortedException) {
// do nothing.
}

self::assertfalse($conn->inTransaction());
self::assertNull($conn->getCurrentTransaction());
self::assertSame(0, $conn->transactionLevel());
}
}
Loading