Skip to content

Commit 48e299b

Browse files
committed
with transactions
1 parent 8a536fe commit 48e299b

2 files changed

Lines changed: 33 additions & 18 deletions

File tree

src/Database/Adapter.php

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@
44

55
use Exception;
66
use Utopia\Database\Exception as DatabaseException;
7+
use Utopia\Database\Exception\Authorization as AuthorizationException;
8+
use Utopia\Database\Exception\Conflict as ConflictException;
79
use Utopia\Database\Exception\Duplicate as DuplicateException;
10+
use Utopia\Database\Exception\Limit as LimitException;
11+
use Utopia\Database\Exception\Relationship as RelationshipException;
12+
use Utopia\Database\Exception\Restricted as RestrictedException;
813
use Utopia\Database\Exception\Timeout as TimeoutException;
914
use Utopia\Database\Exception\Transaction as TransactionException;
1015

@@ -371,7 +376,10 @@ public function inTransaction(): bool
371376
*/
372377
public function withTransaction(callable $callback): mixed
373378
{
374-
for ($attempts = 0; $attempts < 3; $attempts++) {
379+
$sleep = 50_000; // 50 milliseconds
380+
$retries = 2;
381+
382+
for ($attempts = 0; $attempts <= $retries; $attempts++) {
375383
try {
376384
$this->startTransaction();
377385
$result = $callback();
@@ -380,18 +388,31 @@ public function withTransaction(callable $callback): mixed
380388
} catch (\Throwable $action) {
381389
try {
382390
$this->rollbackTransaction();
391+
392+
if (
393+
$action instanceof DuplicateException ||
394+
$action instanceof RestrictedException ||
395+
$action instanceof AuthorizationException ||
396+
$action instanceof RelationshipException ||
397+
$action instanceof ConflictException ||
398+
$action instanceof LimitException
399+
) {
400+
$this->inTransaction = 0;
401+
throw $action;
402+
}
403+
383404
} catch (\Throwable $rollback) {
384-
if ($attempts < 2) {
385-
\usleep(5000); // 5ms
405+
if ($attempts < $retries) {
406+
\usleep($sleep * ($attempts + 1));
386407
continue;
387408
}
388409

389410
$this->inTransaction = 0;
390411
throw $rollback;
391412
}
392413

393-
if ($attempts < 2) {
394-
\usleep(5000); // 5ms
414+
if ($attempts < $retries) {
415+
\usleep($sleep * ($attempts + 1));
395416
continue;
396417
}
397418

src/Database/Adapter/SQL.php

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,17 @@ public function startTransaction(): bool
6666
$this->getPDO()->prepare('ROLLBACK')->execute();
6767
}
6868

69-
$result = $this->getPDO()->beginTransaction();
69+
$this->getPDO()->beginTransaction();
70+
7071
} else {
71-
$result = $this->getPDO()->exec('SAVEPOINT transaction' . $this->inTransaction);
72+
$this->getPDO()->exec('SAVEPOINT transaction' . $this->inTransaction);
7273
}
7374
} catch (PDOException $e) {
7475
throw new TransactionException('Failed to start transaction: ' . $e->getMessage(), $e->getCode(), $e);
7576
}
7677

77-
if (!$result) {
78-
throw new TransactionException('Failed to start transaction');
79-
}
80-
8178
$this->inTransaction++;
79+
8280
return true;
8381
}
8482

@@ -124,21 +122,17 @@ public function rollbackTransaction(): bool
124122

125123
try {
126124
if ($this->inTransaction > 1) {
127-
$result = $this->getPDO()->exec('ROLLBACK TO transaction' . ($this->inTransaction - 1));
125+
$this->getPDO()->exec('ROLLBACK TO transaction' . ($this->inTransaction - 1));
128126
$this->inTransaction--;
129127
} else {
130-
$result = $this->getPDO()->rollBack();
128+
$this->getPDO()->rollBack();
131129
$this->inTransaction = 0;
132130
}
133131
} catch (PDOException $e) {
134132
throw new DatabaseException('Failed to rollback transaction: ' . $e->getMessage(), $e->getCode(), $e);
135133
}
136134

137-
if (!$result) {
138-
throw new TransactionException('Failed to rollback transaction');
139-
}
140-
141-
return $result;
135+
return true;
142136
}
143137

144138
/**

0 commit comments

Comments
 (0)