Skip to content

Commit

Permalink
Fix for doctrine#7068: EntityManager::find() with pessimistic lock sh…
Browse files Browse the repository at this point in the history
…ould check for transaction
  • Loading branch information
Michael Kühn authored and Majkl578 committed Jul 3, 2018
1 parent 2d194de commit 0cbc87a
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 10 deletions.
35 changes: 25 additions & 10 deletions lib/Doctrine/ORM/EntityManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Doctrine\ORM\Exception\MissingIdentifierField;
use Doctrine\ORM\Exception\MissingMappingDriverImplementation;
use Doctrine\ORM\Exception\UnrecognizedIdentifierFields;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\ClassMetadataFactory;
use Doctrine\ORM\Proxy\Factory\ProxyFactory;
use Doctrine\ORM\Proxy\Factory\StaticProxyFactory;
Expand Down Expand Up @@ -376,6 +377,10 @@ public function find($entityName, $id, $lockMode = null, $lockVersion = null)
$class = $this->metadataFactory->getMetadataFor(ltrim($entityName, '\\'));
$className = $class->getClassName();

if ($lockMode !== null) {
$this->checkLockRequirements($lockMode, $class);
}

if (! is_array($id)) {
if ($class->isIdentifierComposite()) {
throw ORMInvalidArgumentException::invalidCompositeIdentifier();
Expand Down Expand Up @@ -438,24 +443,14 @@ public function find($entityName, $id, $lockMode = null, $lockVersion = null)

switch (true) {
case $lockMode === LockMode::OPTIMISTIC:
if (! $class->isVersioned()) {
throw OptimisticLockException::notVersioned($className);
}

$entity = $persister->load($sortedId);

$unitOfWork->lock($entity, $lockMode, $lockVersion);

return $entity;

case $lockMode === LockMode::PESSIMISTIC_READ:
case $lockMode === LockMode::PESSIMISTIC_WRITE:
if (! $this->getConnection()->isTransactionActive()) {
throw TransactionRequiredException::transactionRequired();
}

return $persister->load($sortedId, null, null, [], $lockMode);

default:
return $persister->loadById($sortedId);
}
Expand Down Expand Up @@ -897,4 +892,24 @@ public function hasFilters()
{
return $this->filterCollection !== null;
}

/**
* @throws OptimisticLockException
* @throws TransactionRequiredException
*/
private function checkLockRequirements(int $lockMode, ClassMetadata $class) : void
{
switch ($lockMode) {
case LockMode::OPTIMISTIC:
if (! $class->isVersioned()) {
throw OptimisticLockException::notVersioned($class->getClassName());
}
// Intentional fallthrough
case LockMode::PESSIMISTIC_READ:
case LockMode::PESSIMISTIC_WRITE:
if (! $this->getConnection()->isTransactionActive()) {
throw TransactionRequiredException::transactionRequired();
}
}
}
}
47 changes: 47 additions & 0 deletions tests/Doctrine/Tests/ORM/Functional/Ticket/GH7068Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket;

use Doctrine\DBAL\LockMode;
use Doctrine\ORM\Annotation as ORM;
use Doctrine\ORM\TransactionRequiredException;
use Doctrine\Tests\OrmFunctionalTestCase;

final class GH7068Test extends OrmFunctionalTestCase
{
/**
* {@inheritDoc}
*/
protected function setUp() : void
{
parent::setUp();

$this->setUpEntitySchema(
[
SomeEntity::class,
]
);
}

public function testLockModeIsRespected() : void
{
$entity = new SomeEntity();
$this->em->persist($entity);
$this->em->flush();
$this->em->clear();

$this->em->find(SomeEntity::class, 1);

$this->expectException(TransactionRequiredException::class);
$this->em->find(SomeEntity::class, 1, LockMode::PESSIMISTIC_WRITE);
}
}

/** @ORM\Entity */
final class SomeEntity
{
/** @ORM\Id @ORM\Column(type="integer") @ORM\GeneratedValue */
public $id;
}

0 comments on commit 0cbc87a

Please sign in to comment.