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
13 changes: 12 additions & 1 deletion Spanner/src/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -964,14 +964,25 @@ public function runTransaction(callable $operation, array $options = []): mixed
$this->isRunningTransaction = true;
try {
$res = call_user_func($operation, $transaction);
} catch (\Throwable $e) {
$active = $transaction->state() === Transaction::STATE_ACTIVE;
$singleUse = $transaction->type() === Transaction::TYPE_SINGLE_USE;
if ($active && !$singleUse) {
try {
$transaction->rollback();
} catch (\Throwable $rollbackException) {
// ignore rollback failure and bubble up the original exception
}
}
throw $e;
} finally {
$this->isRunningTransaction = false;
}

$active = $transaction->state() === Transaction::STATE_ACTIVE;
$singleUse = $transaction->type() === Transaction::TYPE_SINGLE_USE;
if ($active && !$singleUse) {
$transaction->rollback($options);
$transaction->rollback();
throw new \RuntimeException('Transactions must be rolled back or committed.');
}

Expand Down
36 changes: 36 additions & 0 deletions Spanner/tests/Unit/DatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,20 @@ public function testRunTransactionNoCommit()
$this->database->runTransaction($this->noop());
}

public function testRunTransactionNoCommitWithTag()
{
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('Transactions must be rolled back or committed.');

$sql = $this->createStreamingAPIArgs()['sql'];
$this->stubExecuteStreamingSql();
$this->spannerClient->rollback(Argument::cetera())->shouldBeCalled();

$this->database->runTransaction(function (Transaction $t) use ($sql) {
$t->execute($sql);
}, ['tag' => self::TRANSACTION_TAG]);
}

public function testRunTransactionNestedTransaction()
{
$this->expectException(BadMethodCallException::class);
Expand Down Expand Up @@ -2288,6 +2302,28 @@ public function testRunTransactionWithRollback()
}, ['tag' => self::TRANSACTION_TAG]);
}

public function testRunTransactionRollsBackOnException()
{
$sql = $this->createStreamingAPIArgs()['sql'];

$this->stubExecuteStreamingSql();
$this->spannerClient->rollback(
Argument::that(function ($request) use ($sql) {
return $request->getTransactionId() == self::TRANSACTION;
}),
Argument::type('array')
)
->shouldBeCalledOnce();

$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('Callback exception');

$this->database->runTransaction(function (Transaction $t) use ($sql) {
$t->execute($sql);
throw new \RuntimeException('Callback exception');
}, ['tag' => self::TRANSACTION_TAG]);
}

public function testRunTransactionWithExcludeTxnFromChangeStreams()
{
$sql = 'SELECT example FROM sql_query';
Expand Down
Loading