Skip to content

Commit 029c498

Browse files
committed
Fix Mongo withTransaction nested call state corruption
1 parent d2593e2 commit 029c498

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/Database/Adapter/Mongo.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,13 @@ public function withTransaction(callable $callback): mixed
103103
{
104104
// If the database is not a replica set, we can't use transactions
105105
if (!$this->client->isReplicaSet()) {
106-
$result = $callback();
107-
return $result;
106+
return $callback();
107+
}
108+
109+
// MongoDB doesn't support nested transactions/savepoints.
110+
// If already in a transaction, just run the callback directly.
111+
if ($this->inTransaction > 0) {
112+
return $callback();
108113
}
109114

110115
try {

tests/e2e/Adapter/Scopes/GeneralTests.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Utopia\Cache\Adapter\Redis as RedisAdapter;
88
use Utopia\Cache\Cache;
99
use Utopia\CLI\Console;
10+
use Utopia\Database\Adapter\Mongo;
1011
use Utopia\Database\Database;
1112
use Utopia\Database\Document;
1213
use Utopia\Database\Exception as DatabaseException;
@@ -927,12 +928,20 @@ public function testTransactionStateAfterRetriesExhausted(): void
927928
/**
928929
* Test that nested withTransaction calls maintain correct inTransaction state
929930
* when the inner transaction throws a known exception.
931+
*
932+
* MongoDB does not support nested transactions or savepoints, so a duplicate
933+
* key error inside an inner transaction aborts the entire transaction.
930934
*/
931935
public function testNestedTransactionState(): void
932936
{
933937
/** @var Database $database */
934938
$database = $this->getDatabase();
935939

940+
if ($database->getAdapter() instanceof Mongo) {
941+
$this->expectNotToPerformAssertions();
942+
return;
943+
}
944+
936945
$database->createCollection('txNested');
937946
$database->createAttribute('txNested', 'title', Database::VAR_STRING, 128, true);
938947

0 commit comments

Comments
 (0)