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 transactions #471

Merged
merged 10 commits into from
Nov 5, 2024
38 changes: 29 additions & 9 deletions src/Database/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Utopia\Database\Exception as DatabaseException;
use Utopia\Database\Exception\Duplicate as DuplicateException;
use Utopia\Database\Exception\Timeout as TimeoutException;
use Utopia\Database\Exception\Transaction as TransactionException;

abstract class Adapter
{
Expand Down Expand Up @@ -315,16 +316,35 @@ public function inTransaction(): bool
*/
public function withTransaction(callable $callback): mixed
{
$this->startTransaction();

try {
$result = $callback();
$this->commitTransaction();
return $result;
} catch (\Throwable $e) {
$this->rollbackTransaction();
throw $e;
for ($attempts = 0; $attempts < 3; $attempts++) {
try {
$this->startTransaction();
$result = $callback();
$this->commitTransaction();
return $result;
} catch (\Throwable $action) {

try {
$this->rollbackTransaction();
} catch (\Throwable $rollback) {
if ($attempts < 2) {
\usleep(5000); // 5ms
continue;
}

throw $rollback;
}

if ($attempts < 2) {
\usleep(5000); // 5ms
continue;
}

throw $action;
}
}

throw new TransactionException('Failed to execute transaction');
}

/**
Expand Down
52 changes: 52 additions & 0 deletions src/Database/Adapter/Postgres.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Utopia\Database\Exception as DatabaseException;
use Utopia\Database\Exception\Duplicate as DuplicateException;
use Utopia\Database\Exception\Timeout as TimeoutException;
use Utopia\Database\Exception\Transaction as TransactionException;
use Utopia\Database\Exception\Truncate as TruncateException;
use Utopia\Database\Query;
use Utopia\Database\Validator\Authorization;
Expand All @@ -26,6 +27,57 @@ class Postgres extends SQL
* 4. Full-text search is different - to_tsvector() and to_tsquery()
*/

/**
* @inheritDoc
*/
public function startTransaction(): bool
{
try {
if ($this->inTransaction === 0) {
if ($this->getPDO()->inTransaction()) {
$this->getPDO()->rollBack();
}

$result = $this->getPDO()->beginTransaction();
} else {
$result = true;
}
} catch (PDOException $e) {
throw new TransactionException('Failed to start transaction: ' . $e->getMessage(), $e->getCode(), $e);
}

if (!$result) {
throw new TransactionException('Failed to start transaction');
}

$this->inTransaction++;

return $result;
}

/**
* @inheritDoc
*/
public function rollbackTransaction(): bool
{
if ($this->inTransaction === 0) {
return false;
}

try {
$result = $this->getPDO()->rollBack();
$this->inTransaction = 0;
} catch (PDOException $e) {
throw new DatabaseException('Failed to rollback transaction: ' . $e->getMessage(), $e->getCode(), $e);
}

if (!$result) {
throw new TransactionException('Failed to rollback transaction');
}

return $result;
}

/**
* Returns Max Execution Time
* @param int $milliseconds
Expand Down
39 changes: 28 additions & 11 deletions src/Database/Adapter/SQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Utopia\Database\Database;
use Utopia\Database\Document;
use Utopia\Database\Exception as DatabaseException;
use Utopia\Database\Exception\Transaction as TransactionException;
use Utopia\Database\Query;

abstract class SQL extends Adapter
Expand Down Expand Up @@ -36,18 +37,23 @@ public function startTransaction(): bool
if ($this->inTransaction === 0) {
if ($this->getPDO()->inTransaction()) {
$this->getPDO()->rollBack();
} else {
// If no active transaction, this has no effect.
$this->getPDO()->prepare('ROLLBACK')->execute();
}

$result = $this->getPDO()->beginTransaction();
} else {
$result = true;
$result = $this->getPDO()
->prepare('SAVEPOINT transaction' . $this->inTransaction)
->execute();
}
} catch (PDOException $e) {
throw new DatabaseException('Failed to start transaction: ' . $e->getMessage(), $e->getCode(), $e);
throw new TransactionException('Failed to start transaction: ' . $e->getMessage(), $e->getCode(), $e);
}

if (!$result) {
throw new DatabaseException('Failed to start transaction');
throw new TransactionException('Failed to start transaction');
}

$this->inTransaction++;
Expand All @@ -67,16 +73,20 @@ public function commitTransaction(): bool
return true;
}

if (!$this->getPDO()->inTransaction()) {
$this->inTransaction = 0;
return false;
}

try {
$result = $this->getPDO()->commit();
$this->inTransaction = 0;
} catch (PDOException $e) {
throw new DatabaseException('Failed to commit transaction: ' . $e->getMessage(), $e->getCode(), $e);
} finally {
$this->inTransaction--;
throw new TransactionException('Failed to commit transaction: ' . $e->getMessage(), $e->getCode(), $e);
}

if (!$result) {
throw new DatabaseException('Failed to commit transaction');
throw new TransactionException('Failed to commit transaction');
}

return $result;
Expand All @@ -92,15 +102,22 @@ public function rollbackTransaction(): bool
}

try {
$result = $this->getPDO()->rollBack();
if ($this->inTransaction > 1) {
$result = $this->getPDO()
->prepare('ROLLBACK TO transaction' . ($this->inTransaction - 1))
->execute();

$this->inTransaction--;
} else {
$result = $this->getPDO()->rollBack();
$this->inTransaction = 0;
}
} catch (PDOException $e) {
throw new DatabaseException('Failed to rollback transaction: ' . $e->getMessage(), $e->getCode(), $e);
} finally {
$this->inTransaction = 0;
}

if (!$result) {
throw new DatabaseException('Failed to rollback transaction');
throw new TransactionException('Failed to rollback transaction');
}

return $result;
Expand Down
31 changes: 31 additions & 0 deletions src/Database/Adapter/SQLite.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Utopia\Database\Exception\Duplicate as DuplicateException;
use Utopia\Database\Exception\NotFound as NotFoundException;
use Utopia\Database\Exception\Timeout as TimeoutException;
use Utopia\Database\Exception\Transaction as TransactionException;
use Utopia\Database\Helpers\ID;

/**
Expand All @@ -30,6 +31,36 @@
*/
class SQLite extends MariaDB
{
/**
* @inheritDoc
*/
public function startTransaction(): bool
{
try {
if ($this->inTransaction === 0) {
if ($this->getPDO()->inTransaction()) {
$this->getPDO()->rollBack();
}

$result = $this->getPDO()->beginTransaction();
} else {
$result = $this->getPDO()
->prepare('SAVEPOINT transaction' . $this->inTransaction)
->execute();
}
} catch (PDOException $e) {
throw new TransactionException('Failed to start transaction: ' . $e->getMessage(), $e->getCode(), $e);
}

if (!$result) {
throw new TransactionException('Failed to start transaction');
}

$this->inTransaction++;

return $result;
}

/**
* Check if Database exists
* Optionally check if collection exists in Database
Expand Down
9 changes: 9 additions & 0 deletions src/Database/Exception/Transaction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Utopia\Database\Exception;

use Utopia\Database\Exception;

class Transaction extends Exception
{
}
Loading