title: Database pessimistic locking for concurrent reads using Doctrine and Azure SQL
body: <p><strong>WHAT I'M TRYING TO DO</strong></p>
  
  <p>I have a cloud application with multiple worker nodes that can each process n export requests. Export requests are placed into tables in our database. Each export request should only ever be run once. If a worker node is currently running less than n export requests, it checks every 0.1 - 1 seconds to see if there's a export request it can start processing. </p>
  
  <p>The export requests that are waiting to be run are organised into a priority queue on the database side using a stored procedure. The below function (<code>queuePop()</code>) is the function that calls the database. It gets the top result from the priority queue, marks the export request as being used (it's consider being used when the status is no longer set to 'NEW') and then returns the export request to be processed elsewhere.</p>
  
  <pre><code>public function queuePop() {
  
      $entity_manager-&gt;getConnection()-&gt;beginTransaction();
  
      // Get every new export request
      try {
  
          $statment = $entity_manager-&gt;getConnection()-&gt;prepare( 'EXEC root.findAllNewExportRequests' );
          $statment-&gt;execute();
          $results = $statment-&gt;fetchAll();
  
          if ( count( $results ) &gt; 0 ) {
  
              $queue_item = array (
                      'export_request_id' =&gt; $results[0]['request_id']
              );
          } else {
  
              $entity_manager-&gt;getConnection()-&gt;commit();
              return NULL;
          }
  
          // Return the export request if it exists
          $export_request = $entity_manager-&gt;find( 'Application\Model\ExportRequest', $queue_item['export_request_id'], \Doctrine\DBAL\LockMode::PESSIMISTIC_READ );
  
          if ( !$export_request ) {
  
              // Export request not found
              throw new Exception( 'Export request not found.' );
          }
  
          // Update the export request
          $export_request-&gt;setStatus( 'QUEUED' );
  
          // Update the export request in the database
          $entity_manager-&gt;persist( $export_request );
          $entity_manager-&gt;flush();
  
          $entity_manager-&gt;getConnection()-&gt;commit();
          return $queue_item;
      } catch ( \Exception $ex ) {
  
          $entity_manager-&gt;getConnection()-&gt;rollback();
          exit();
      }
  }
  </code></pre>
  
  <p><strong>THE PROBLEM</strong></p>
  
  <p>The queuePop function works great and it ensures that a single worker node will never run an export request more than once, but when there's 2 or more nodes there's a chance that more than one worker node will pick up the request (it happens often enough given the number of requests it gets). I tried implementing pessimistic locking as seen here (<a href="http://doctrine-orm.readthedocs.org/en/latest/reference/transactions-and-concurrency.html#pessimistic-locking" rel="nofollow">http://doctrine-orm.readthedocs.org/en/latest/reference/transactions-and-concurrency.html#pessimistic-locking</a>) but it isn't working. The docs aren't very good and I can't find any examples of pessimistic locking being used properly. </p>
  
  <p>I need for when one worker node is reading/updating an export request, that the other nodes can't (they have to wait).</p>
  
  <p><strong>WHAT I'M USING</strong></p>
  
  <ul>
  <li>nginx version: nginx/1.4.6 (Ubuntu)</li>
  <li>Ubuntu 14.04.1 LTS (GNU/Linux 3.13.0-32-generix x86_64)</li>
  <li>PHP 5.5.9-1ubuntu4.3 (fpm-fcgi)</li>
  <li>Zend Engine v2.5.0 with Zend OPcache v7.0.3</li>
  <li>Doctrine 2</li>
  <li>Azure SQL</li>
  </ul>