|
16 | 16 | use Doctrine\DBAL\Driver\Result as DriverResult;
|
17 | 17 | use Doctrine\DBAL\Exception;
|
18 | 18 | use Doctrine\DBAL\Exception\TableNotFoundException;
|
| 19 | +use Doctrine\DBAL\LockMode; |
19 | 20 | use Doctrine\DBAL\Query\QueryBuilder;
|
20 | 21 | use Doctrine\DBAL\Result;
|
21 | 22 | use Doctrine\DBAL\Schema\Comparator;
|
@@ -172,9 +173,23 @@ public function get(): ?array
|
172 | 173 | ->orderBy('available_at', 'ASC')
|
173 | 174 | ->setMaxResults(1);
|
174 | 175 |
|
| 176 | + // Append pessimistic write lock to FROM clause if db platform supports it |
| 177 | + $sql = $query->getSQL(); |
| 178 | + if (($fromPart = $query->getQueryPart('from')) && |
| 179 | + ($table = $fromPart[0]['table'] ?? null) && |
| 180 | + ($alias = $fromPart[0]['alias'] ?? null) |
| 181 | + ) { |
| 182 | + $fromClause = sprintf('%s %s', $table, $alias); |
| 183 | + $sql = str_replace( |
| 184 | + sprintf('FROM %s WHERE', $fromClause), |
| 185 | + sprintf('FROM %s WHERE', $this->driverConnection->getDatabasePlatform()->appendLockHint($fromClause, LockMode::PESSIMISTIC_WRITE)), |
| 186 | + $sql |
| 187 | + ); |
| 188 | + } |
| 189 | + |
175 | 190 | // use SELECT ... FOR UPDATE to lock table
|
176 | 191 | $stmt = $this->executeQuery(
|
177 |
| - $query->getSQL().' '.$this->driverConnection->getDatabasePlatform()->getWriteLockSQL(), |
| 192 | + $sql.' '.$this->driverConnection->getDatabasePlatform()->getWriteLockSQL(), |
178 | 193 | $query->getParameters(),
|
179 | 194 | $query->getParameterTypes()
|
180 | 195 | );
|
|
0 commit comments