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 incorrect transactional() handling when DB auto-rolled back the transaction (v4) #6546

Merged
merged 1 commit into from
Nov 5, 2024
Merged
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
55 changes: 44 additions & 11 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,17 @@
use Doctrine\DBAL\Connection\StaticServerVersionProvider;
use Doctrine\DBAL\Driver\API\ExceptionConverter;
use Doctrine\DBAL\Driver\Connection as DriverConnection;
use Doctrine\DBAL\Driver\Exception as TheDriverException;
use Doctrine\DBAL\Driver\Statement as DriverStatement;
use Doctrine\DBAL\Exception\CommitFailedRollbackOnly;
use Doctrine\DBAL\Exception\ConnectionLost;
use Doctrine\DBAL\Exception\DeadlockException;
use Doctrine\DBAL\Exception\DriverException;
use Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException;
use Doctrine\DBAL\Exception\NoActiveTransaction;
use Doctrine\DBAL\Exception\SavepointsNotSupported;
use Doctrine\DBAL\Exception\TransactionRolledBack;
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Query\Expression\ExpressionBuilder;
use Doctrine\DBAL\Query\QueryBuilder;
Expand Down Expand Up @@ -931,16 +936,35 @@

try {
$res = $func($this);
$this->commit();

$successful = true;

return $res;
} finally {
if (! $successful) {
$this->rollBack();
}
}

$shouldRollback = true;
try {
$this->commit();

$shouldRollback = false;
} catch (TheDriverException $t) {
$shouldRollback = ! (
$t instanceof TransactionRolledBack
|| $t instanceof UniqueConstraintViolationException
|| $t instanceof ForeignKeyConstraintViolationException
|| $t instanceof DeadlockException
);

Check warning on line 958 in src/Connection.php

View check run for this annotation

Codecov / codecov/patch

src/Connection.php#L952-L958

Added lines #L952 - L958 were not covered by tests

throw $t;

Check warning on line 960 in src/Connection.php

View check run for this annotation

Codecov / codecov/patch

src/Connection.php#L960

Added line #L960 was not covered by tests
} finally {
if ($shouldRollback) {
$this->rollBack();

Check warning on line 963 in src/Connection.php

View check run for this annotation

Codecov / codecov/patch

src/Connection.php#L963

Added line #L963 was not covered by tests
}
}

return $res;
}

/**
Expand Down Expand Up @@ -1019,17 +1043,26 @@

$connection = $this->connect();

if ($this->transactionNestingLevel === 1) {
try {
$connection->commit();
} catch (Driver\Exception $e) {
throw $this->convertException($e);
try {
if ($this->transactionNestingLevel === 1) {
try {
$connection->commit();
} catch (Driver\Exception $e) {

Check warning on line 1050 in src/Connection.php

View check run for this annotation

Codecov / codecov/patch

src/Connection.php#L1050

Added line #L1050 was not covered by tests
throw $this->convertException($e);
}
} else {
$this->releaseSavepoint($this->_getNestedTransactionSavePointName());
}
} else {
$this->releaseSavepoint($this->_getNestedTransactionSavePointName());
} finally {
$this->updateTransactionStateAfterCommit();
}
}

--$this->transactionNestingLevel;
private function updateTransactionStateAfterCommit(): void
{
if ($this->transactionNestingLevel !== 0) {
--$this->transactionNestingLevel;
}

if ($this->autoCommit !== false || $this->transactionNestingLevel !== 0) {
return;
Expand Down
28 changes: 28 additions & 0 deletions src/Driver/API/OCI/ExceptionConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use Doctrine\DBAL\Driver\API\ExceptionConverter as ExceptionConverterInterface;
use Doctrine\DBAL\Driver\Exception;
use Doctrine\DBAL\Driver\OCI8\Exception\Error;
use Doctrine\DBAL\Driver\PDO\Exception as DriverPDOException;
use Doctrine\DBAL\Exception\ConnectionException;
use Doctrine\DBAL\Exception\DatabaseDoesNotExist;
use Doctrine\DBAL\Exception\DatabaseObjectNotFoundException;
Expand All @@ -17,9 +19,15 @@
use Doctrine\DBAL\Exception\SyntaxErrorException;
use Doctrine\DBAL\Exception\TableExistsException;
use Doctrine\DBAL\Exception\TableNotFoundException;
use Doctrine\DBAL\Exception\TransactionRolledBack;
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use Doctrine\DBAL\Query;

use function assert;
use function count;
use function explode;
use function str_replace;

/** @internal */
final class ExceptionConverter implements ExceptionConverterInterface
{
Expand All @@ -40,6 +48,26 @@
12545 => new ConnectionException($exception, $query),
1400 => new NotNullConstraintViolationException($exception, $query),
1918 => new DatabaseDoesNotExist($exception, $query),
2091 => (function () use ($exception, $query) {

Check warning on line 51 in src/Driver/API/OCI/ExceptionConverter.php

View check run for this annotation

Codecov / codecov/patch

src/Driver/API/OCI/ExceptionConverter.php#L51

Added line #L51 was not covered by tests
//SQLSTATE[HY000]: General error: 2091 OCITransCommit: ORA-02091: transaction rolled back
//ORA-00001: unique constraint (DOCTRINE.GH3423_UNIQUE) violated
$lines = explode("\n", $exception->getMessage(), 2);

Check warning on line 54 in src/Driver/API/OCI/ExceptionConverter.php

View check run for this annotation

Codecov / codecov/patch

src/Driver/API/OCI/ExceptionConverter.php#L54

Added line #L54 was not covered by tests
assert(count($lines) >= 2);

[, $causeError] = $lines;

Check warning on line 57 in src/Driver/API/OCI/ExceptionConverter.php

View check run for this annotation

Codecov / codecov/patch

src/Driver/API/OCI/ExceptionConverter.php#L57

Added line #L57 was not covered by tests

[$causeCode] = explode(': ', $causeError, 2);
$code = (int) str_replace('ORA-', '', $causeCode);

Check warning on line 60 in src/Driver/API/OCI/ExceptionConverter.php

View check run for this annotation

Codecov / codecov/patch

src/Driver/API/OCI/ExceptionConverter.php#L59-L60

Added lines #L59 - L60 were not covered by tests

$sqlState = $exception->getSQLState();
if ($exception instanceof DriverPDOException) {
$why = $this->convert(new DriverPDOException($causeError, $sqlState, $code, $exception), $query);

Check warning on line 64 in src/Driver/API/OCI/ExceptionConverter.php

View check run for this annotation

Codecov / codecov/patch

src/Driver/API/OCI/ExceptionConverter.php#L62-L64

Added lines #L62 - L64 were not covered by tests
} else {
$why = $this->convert(new Error($causeError, $sqlState, $code, $exception), $query);

Check warning on line 66 in src/Driver/API/OCI/ExceptionConverter.php

View check run for this annotation

Codecov / codecov/patch

src/Driver/API/OCI/ExceptionConverter.php#L66

Added line #L66 was not covered by tests
}

return new TransactionRolledBack($why, $query);
})(),

Check warning on line 70 in src/Driver/API/OCI/ExceptionConverter.php

View check run for this annotation

Codecov / codecov/patch

src/Driver/API/OCI/ExceptionConverter.php#L69-L70

Added lines #L69 - L70 were not covered by tests
2289,
2443,
4080 => new DatabaseObjectNotFoundException($exception, $query),
Expand Down
2 changes: 1 addition & 1 deletion src/Driver/OCI8/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@

public function commit(): void
{
if (! oci_commit($this->connection)) {
if (! @oci_commit($this->connection)) {

Check warning on line 98 in src/Driver/OCI8/Connection.php

View check run for this annotation

Codecov / codecov/patch

src/Driver/OCI8/Connection.php#L98

Added line #L98 was not covered by tests
throw Error::new($this->connection);
}

Expand Down
1 change: 1 addition & 0 deletions src/Driver/OCI8/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use function oci_error;
use function oci_fetch_all;
use function oci_fetch_array;
use function oci_field_name;
use function oci_num_fields;
use function oci_num_rows;

Expand Down
10 changes: 10 additions & 0 deletions src/Exception/TransactionRolledBack.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Doctrine\DBAL\Exception;

/** @psalm-immutable */
class TransactionRolledBack extends DriverException
{
}
22 changes: 22 additions & 0 deletions tests/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -675,4 +675,26 @@ public function rollBack(): void
self::assertSame('Original exception', $e->getPrevious()->getMessage());
}
}

/**
* We are not sure if this can happen in real life scenario
*/
public function testItFailsDuringCommitBeforeTouchingDb(): void
{
$connection = new class (['memory' => true], new Driver\SQLite3\Driver()) extends Connection {
public function commit(): void
{
throw new \Exception('Fail before touching the db');
}

public function rollBack(): void
{
throw new \Exception('Rollback got triggered');
}
};

$this->expectExceptionMessage('Rollback got triggered');
$connection->transactional(static function (): void {
});
}
}
Loading