Uploaded image for project: 'Doctrine 2 - ORM'
  1. Doctrine 2 - ORM
  2. DDC-1846

Pessimistic lock does not retreive latest version of entity when entity is already in doctrine cache


    • Type: Bug
    • Status: Resolved
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: Git Master
    • Fix Version/s: 2.2.3
    • Component/s: ORM
    • Security Level: All
    • Labels:
    • Environment:
      Mysql 5.5.22-0ubuntu1


      When setting a pessimistic lock on an entity (e.g. a row lock) and retreiving an entity from the database, Doctrine returns the entity from cache if it has any. When updating a counter on an entity for example, it is important the row is locked, retreived, updated and then unlocked to guarantee the counter keeps in sync. Using $em->clear(); before $em->find(); is a work-around for this problem, but as requested by Benjamin Eberlei on the google groups thread (https://groups.google.com/forum/?fromgroups#!topic/doctrine-user/N8Xop2-XbTY) this bugreport is made to fix this without the need of clear().

      In the next example, if the entity is previously retreived already, find() returns that version instead of retreiving the current version from the database after the rowlock is set. When $em->clear(); can be used to work-around the problem

      // $em instanceof EntityManager
      //$em->clear(); // Uncommenting this fixed the problem
      try {
      	$entity = $em->find('Entity', $id, LockMode::PESSIMISTIC_WRITE);
      	/* Update some fields, for example, decrease a counter */
      	$entity->setCounter($entity->getCounter() - 1);
      } catch ( \Exception $ex ) {

        Issue Links



            • Assignee:
              beberlei Benjamin Eberlei
              bramklg Bram Klein Gunnewiek
            • Votes:
              0 Vote for this issue
              4 Start watching this issue


              • Created: