title: How to lock a whole table in symfony2 with doctrine2?
body: <p>I need to lock a whole table (not a single row) with doctrine, I would like to do this without native queries if possible.</p>
  
  <p>The documentation for <a href="http://doctrine-orm.readthedocs.io/projects/doctrine-orm/en/latest/reference/transactions-and-concurrency.html" rel="nofollow noreferrer">pessimistic locking</a> only describes how to lock specific entities through these methods:</p>
  
  <ul>
  <li>EntityManager#find</li>
  <li>EntityManager#lock</li>
  <li>Query#setLockMode</li>
  </ul>
  
  <p>I have a transaction that needs to insert a row whose values depend on the values of the rest of the rows in the table, so I need to prevent two transactions performing at the same time on that table.</p>
  
  <p>I'm using explicit transaction demarcation, which is supposed to work well with locking (according to the documentation above).</p>
  
  <p><strong>NOTE</strong>: Optimistic locking is not good enough in this case, I can't afford retrying the transaction. Besides the query is not supposed to be slow so performance isn't an issue.</p>
  
  <p><strong>EDIT</strong>: I'll give an example. Imagine you want to hand build an auto_increment, and you have to select max() from the table to get the previous result in order to insert the next one. You have to make sure that no two transactions try to insert the same value in case they select max() at the same time.</p>
  
  <p>I'm looking for a general solution to this problem when auto_increment is not good, for example with strings, or multiple columns, hashes or whatever calculation you have to make on the previous row set.</p>
  
  <p>Locking is a solid solution and, unlike optimistic locking, you don't have to retry on errors.</p>
  
  <p>So, is there any way to use table locking in doctrine?</p>