I am trying to shut down Pessimistic Locking with Doctrine ORM for PostgreSql. Doctrine and PostgreSql with default configuration.
Sample code (symfony command).
$sleep - waiting time in seconds
$manager = $this->getContainer()->get('mmi.manager.message'); $conn = $manager->em()->getConnection(); $manager->em()->getConnection()->beginTransaction(); try { $entity = $manager->repo()->find('cd7eb9e9', LockMode::PESSIMISTIC_WRITE); $entity->setState(EntityActionInterface::STATE_IN_PROGRESS); $manager->em()->persist($entity); $manager->em()->flush(); $ts = (new \DateTime())->getTimestamp(); $output->writeln("TS: {$ts}"); if ($sleep) { $output->writeln("Sleep: {$sleep}"); sleep($sleep); } $entity->setMessage([$ts]); $manager->em()->persist($entity); $manager->em()->flush(); $conn->commit(); } catch (PessimisticLockException $ex) { var_dump(get_class($ex)); $conn->rollBack(); throw $ex; } catch (\Exception $ex) { var_dump(get_class($ex)); $conn->rollBack(); throw $ex; } How tested
I run 2 teams. The first start-up and waits 20 seconds. The second start-up without waiting.
Expected result
The second command throws a PessimisticLockException exception.
Actual result
The second team waits until the transaction from the first team commits, and then overwrites the record.
Question
How to make the doctrine spit out a PessimisticLockException if the received entry is locked?