Created
September 4, 2023 14:08
-
-
Save someniatko/60c26399cf83a613c3c852f11a8ca76f to your computer and use it in GitHub Desktop.
DBAL Middleware for Transaction Isolation Level
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
namespace MyProject\Database; | |
use Doctrine\Bundle\DoctrineBundle; | |
use Doctrine\DBAL\Connection; | |
use Doctrine\DBAL\Driver; | |
use Doctrine\DBAL\Driver\API\ExceptionConverter; | |
use Doctrine\DBAL\Driver\Connection as DriverConnection; | |
use Doctrine\DBAL\Driver\Middleware; | |
use Doctrine\DBAL\Platforms\AbstractPlatform; | |
use Doctrine\DBAL\Schema\AbstractSchemaManager; | |
use Doctrine\DBAL\TransactionIsolationLevel; | |
use SensitiveParameter; | |
#[DoctrineBundle\Attribute\AsMiddleware] | |
final class TransactionIsolationLevelDoctrineMiddleware implements Middleware | |
{ | |
public function wrap(Driver $driver): Driver | |
{ | |
return new class ($driver) implements Driver { | |
public function __construct( | |
private Driver $wrappedDriver, | |
) {} | |
public function connect(#[SensitiveParameter] array $params): DriverConnection | |
{ | |
$connection = $this->wrappedDriver->connect($params); | |
$connection->exec( | |
$this->wrappedDriver->getDatabasePlatform() | |
->getSetTransactionIsolationSQL(TransactionIsolationLevel::READ_COMMITTED) | |
); | |
return $connection; | |
} | |
public function getDatabasePlatform(): AbstractPlatform | |
{ | |
return $this->wrappedDriver->getDatabasePlatform(); | |
} | |
public function getSchemaManager(Connection $conn, AbstractPlatform $platform): AbstractSchemaManager | |
{ | |
/** @psalm-suppress DeprecatedMethod */ | |
return $this->wrappedDriver->getSchemaManager($conn, $platform); | |
} | |
public function getExceptionConverter(): ExceptionConverter | |
{ | |
return $this->wrappedDriver->getExceptionConverter(); | |
} | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment