Skip to content

Commit

Permalink
[Messenger] Fix mssql compatibility for doctrine transport.
Browse files Browse the repository at this point in the history
Add logic for locking row for update when the doctrine dbal connection is sqlsrv. This is a quick and dirty solution, but it prevents the need to rewrite the logic due to doctrine dbal limitations.

See issue symfony/symfony#39117
  • Loading branch information
bill moll authored and derrabus committed Nov 28, 2020
1 parent 57d9c19 commit 7ada2e6
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion Transport/Doctrine/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Doctrine\DBAL\Driver\Result as DriverResult;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Exception\TableNotFoundException;
use Doctrine\DBAL\LockMode;
use Doctrine\DBAL\Query\QueryBuilder;
use Doctrine\DBAL\Result;
use Doctrine\DBAL\Schema\Comparator;
Expand Down Expand Up @@ -161,9 +162,23 @@ public function get(): ?array
->orderBy('available_at', 'ASC')
->setMaxResults(1);

// Append pessimistic write lock to FROM clause if db platform supports it
$sql = $query->getSQL();
if (($fromPart = $query->getQueryPart('from')) &&
($table = $fromPart[0]['table'] ?? null) &&
($alias = $fromPart[0]['alias'] ?? null)
) {
$fromClause = sprintf('%s %s', $table, $alias);
$sql = str_replace(
sprintf('FROM %s WHERE', $fromClause),
sprintf('FROM %s WHERE', $this->driverConnection->getDatabasePlatform()->appendLockHint($fromClause, LockMode::PESSIMISTIC_WRITE)),
$sql
);
}

// use SELECT ... FOR UPDATE to lock table
$stmt = $this->executeQuery(
$query->getSQL().' '.$this->driverConnection->getDatabasePlatform()->getWriteLockSQL(),
$sql.' '.$this->driverConnection->getDatabasePlatform()->getWriteLockSQL(),
$query->getParameters(),
$query->getParameterTypes()
);
Expand Down

0 comments on commit 7ada2e6

Please sign in to comment.