Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2.6] Fix for #7068: EntityManager::find() with pessimistic lock should check for transaction #7291

Merged
merged 1 commit into from
Jul 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 27 additions & 8 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,10 +454,6 @@ 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:
Expand Down Expand Up @@ -915,4 +912,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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: we use a space between ) and :. Not important though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, leftover from original PR.

{
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;
}