title: Doctrine deadlock with ORM updates
body: <p>I'm trying to figure out what is causing deadlocks in my Symfony 2 application. I'm running a cronjob that does batch-updates on a fairly large dataset and one part of it causes this error:</p>
  
  <blockquote>
    <p>Doctrine\DBAL\DBALException: An exception occurred while executing
    'UPDATE SpotEvent SET ts = ?, current = ? WHERE id = ?' with params
    ["2015-12-28 00:35:27", 1, 39316]:  SQLSTATE[40P01]: Deadlock
    detected: 7 ERROR:  deadlock detected DETAIL:  Process 32030 waits for
    ShareLock on transaction 2130787; blocked by process 32029. Process
    32029 waits for ShareLock on transaction 2130786; blocked by process
    32030. HINT:  See server log for query details. CONTEXT:  while updating tuple (105,68) in relation "spotevent" (uncaught exception)
    at
    /home/maf/symfony/vendor/doctrine/dbal/lib/Doctrine/DBAL/DBALException.php
    line 91 while running console command</p>
  </blockquote>
  
  <p>The code causing it is basically this:</p>
  
  <pre><code>check event
  if (already in database) {
      update timestamp
  } else {
      create new
  }
  </code></pre>
  
  <p>From what I see in the error, the first branch causes the deadlock, but from what I read about deadlocks, the second should be more likely. In any case I don't understand why I have a deadlock at all.</p>
  
  <p>I should say I am running this job in 6 parallel processes. However, there is no overlap between them (i.e. job one is checking from 1-200, job 2 from 201 to 400, etc.)</p>
  
  <p>I'm using PostgreSQL as the database backend. My "check event" step is done using DQL, everything else is pure ORM.</p>