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 4192c3a commit bfd8571
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 9 deletions.
36 changes: 27 additions & 9 deletions lib/Doctrine/ORM/EntityManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\LockMode;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Query\ResultSetMapping;
use Doctrine\ORM\Proxy\ProxyFactory;
use Doctrine\ORM\Query\FilterCollection;
Expand Down Expand Up @@ -380,6 +381,10 @@ public function find($entityName, $id, $lockMode = null, $lockVersion = null)
{
$class = $this->metadataFactory->getMetadataFor(ltrim($entityName, '\\'));

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

if ( ! is_array($id)) {
if ($class->isIdentifierComposite) {
throw ORMInvalidArgumentException::invalidCompositeIdentifier();
Expand Down Expand Up @@ -441,10 +446,6 @@ public function find($entityName, $id, $lockMode = null, $lockVersion = null)

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

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

$unitOfWork->lock($entity, $lockMode, $lockVersion);
Expand All @@ -453,12 +454,7 @@ public function find($entityName, $id, $lockMode = null, $lockVersion = null)

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

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

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

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

namespace Doctrine\Tests\ORM\Functional\Ticket;

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

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

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

public function testLockModeIsRespected()
{
$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);
}
}

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

0 comments on commit bfd8571

Please sign in to comment.