|
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;
|
@@ -161,9 +162,23 @@ public function get(): ?array
|
161 | 162 | ->orderBy('available_at', 'ASC')
|
162 | 163 | ->setMaxResults(1);
|
163 | 164 |
|
| 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 | + |
164 | 179 | // use SELECT ... FOR UPDATE to lock table
|
165 | 180 | $stmt = $this->executeQuery(
|
166 |
| - $query->getSQL().' '.$this->driverConnection->getDatabasePlatform()->getWriteLockSQL(), |
| 181 | + $sql.' '.$this->driverConnection->getDatabasePlatform()->getWriteLockSQL(), |
167 | 182 | $query->getParameters(),
|
168 | 183 | $query->getParameterTypes()
|
169 | 184 | );
|
|
0 commit comments