DDC-2929: Pessimistic lock on Query does not update the entity with the DB values if it's already cached #3646

Open
opened 2026-01-22 14:24:38 +01:00 by admin · 2 comments
Owner

Originally created by @doctrinebot on GitHub (Jan 22, 2014).

Originally assigned to: @beberlei on GitHub.

Jira issue originally created by user nail:

When asking for a pessimistic lock through a Query object, the entities already in the cache are not refreshed with the latest values from the database (unless you set the Query::HINT_REFRESH hint)

Example:

        $em->beginTransaction();
        try {
            $bar = $em->createQuery('SELECT b FROM Foo:Bar b WHERE b.id = :id')
                        ->setParameter('id', 150)
                        ->getSingleResult();
            var_dump($bar->getAmount()); // Yields some positive value
            $bar->setAmount(0);
            $bar = $em->createQuery('SELECT b FROM Foo:Bar b WHERE b.id = :id')
                        ->setParameter('id', 150)
                        ->setLockMode(\Doctrine\DBAL\LockMode::PESSIMISTIC_WRITE)
                        // ->setHint(\Doctrine\ORM\Query::HINT_REFRESH, true)
                        ->getSingleResult();
            var_dump($bar->getAmount()); // Yields 0

            $em->flush();
            $em->commit();
        } catch (\Exception $e) {
            $em->rollback();
        }

This is similar to DDC-1846

(Tested on 2.4.0 and 2.4.1)

Originally created by @doctrinebot on GitHub (Jan 22, 2014). Originally assigned to: @beberlei on GitHub. Jira issue originally created by user nail: When asking for a pessimistic lock through a Query object, the entities already in the cache are not refreshed with the latest values from the database (unless you set the `Query::HINT_REFRESH` hint) Example: ``` $em->beginTransaction(); try { $bar = $em->createQuery('SELECT b FROM Foo:Bar b WHERE b.id = :id') ->setParameter('id', 150) ->getSingleResult(); var_dump($bar->getAmount()); // Yields some positive value $bar->setAmount(0); $bar = $em->createQuery('SELECT b FROM Foo:Bar b WHERE b.id = :id') ->setParameter('id', 150) ->setLockMode(\Doctrine\DBAL\LockMode::PESSIMISTIC_WRITE) // ->setHint(\Doctrine\ORM\Query::HINT_REFRESH, true) ->getSingleResult(); var_dump($bar->getAmount()); // Yields 0 $em->flush(); $em->commit(); } catch (\Exception $e) { $em->rollback(); } ``` This is similar to [DDC-1846](http://www.doctrine-project.org/jira/browse/DDC-1846) (Tested on 2.4.0 and 2.4.1)
admin added the Bug label 2026-01-22 14:24:38 +01:00
Author
Owner
@doctrinebot commented on GitHub (Jan 22, 2014): - is referenced by [DDC-1846: Pessimistic lock does not retreive latest version of entity when entity is already in doctrine cache](http://www.doctrine-project.org/jira/browse/DDC-1846)
Author
Owner

@lasekio commented on GitHub (May 14, 2016):

Are you guys interested about fix this? It is serious bug, which caused domain inconsistency in my application.

@lasekio commented on GitHub (May 14, 2016): Are you guys interested about fix this? It is serious bug, which caused domain inconsistency in my application.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#3646