Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 37be094

Browse files
bill mollderrabus
bill moll
authored andcommitted
[Messenger] Fix mssql compatibility for doctrine transport.
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 #39117
1 parent 3e9f770 commit 37be094

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

src/Symfony/Component/Messenger/Transport/Doctrine/Connection.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Doctrine\DBAL\Driver\Result as DriverResult;
1717
use Doctrine\DBAL\Exception;
1818
use Doctrine\DBAL\Exception\TableNotFoundException;
19+
use Doctrine\DBAL\LockMode;
1920
use Doctrine\DBAL\Query\QueryBuilder;
2021
use Doctrine\DBAL\Result;
2122
use Doctrine\DBAL\Schema\Comparator;
@@ -161,9 +162,23 @@ public function get(): ?array
161162
->orderBy('available_at', 'ASC')
162163
->setMaxResults(1);
163164

165+
// Append pessimistic write lock to FROM clause if db platform supports it
166+
$sql = $query->getSQL();
167+
if (($fromPart = $query->getQueryPart('from')) &&
168+
($table = $fromPart[0]['table'] ?? null) &&
169+
($alias = $fromPart[0]['alias'] ?? null)
170+
) {
171+
$fromClause = sprintf('%s %s', $table, $alias);
172+
$sql = str_replace(
173+
sprintf('FROM %s WHERE', $fromClause),
174+
sprintf('FROM %s WHERE', $this->driverConnection->getDatabasePlatform()->appendLockHint($fromClause, LockMode::PESSIMISTIC_WRITE)),
175+
$sql
176+
);
177+
}
178+
164179
// use SELECT ... FOR UPDATE to lock table
165180
$stmt = $this->executeQuery(
166-
$query->getSQL().' '.$this->driverConnection->getDatabasePlatform()->getWriteLockSQL(),
181+
$sql.' '.$this->driverConnection->getDatabasePlatform()->getWriteLockSQL(),
167182
$query->getParameters(),
168183
$query->getParameterTypes()
169184
);

0 commit comments

Comments
 (0)