title: Doctrine LockMode::PESSIMISTIC databases repeat
body: <p>I am using Doctring LockMode::PESSIMISTIC_WRITE</p>
  
  <p>but when I using </p>
  
  <blockquote>
    <p>usr/bin/ab -n 100 -c 10</p>
  </blockquote>
  
  <p>my Databases is like this </p>
  
  <p><a href="https://i.stack.imgur.com/aqHhB.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/aqHhB.jpg" alt="enter image description here"></a></p>
  
  <p>some Balance is repeat</p>
  
  <p>I don't know how to fix it</p>
  
  <p>this my controller</p>
  
  <pre><code>/**
       * @Route("/bank/moneyin", name = "money_in")
       * 
       */
      public function moneyIn(Request $request)
      {
          $amount = 10;
          $user = 123;
  
          $entityManager = $this-&gt;getDoctrine()-&gt;getEntityManager();
          $data = $entityManager-&gt;find('BankBundle:user', $user);
  
          $balance = $data-&gt;getBalance() + $amount;
          if ($balance &lt; 0)
          {
              $ret = ['result' =&gt; 'error', 'ret' =&gt; '餘額不足'];
          }
  
          if ($balance &gt;= 0)
          {
              $entityManager-&gt;getConnection()-&gt;beginTransaction();
              try 
              {
                  $user = $entityManager-&gt;find('BankBundle:user', $user, LockMode::PESSIMISTIC_WRITE);
                  $user-&gt;setBalance($balance);
                  // $entityManager-&gt;persist($user);
                  $bank = new entry();
                  $bank-&gt;setUser($user);
                  $bank-&gt;setAmount($amount);
                  $bank-&gt;setBalance($balance);
                  $entityManager-&gt;persist($bank);
                  $entityManager-&gt;flush();
                  $bankdata = $bank-&gt;toArray();
                  $entityManager-&gt;getConnection()-&gt;commit();
                  $ret = ['result' =&gt; 'ok', 'ret' =&gt; $bankdata];
                  return new JsonResponse($ret);
              }
              catch(Exception $e) 
              {
                  $entityManager-&gt;getConnection()-&gt;rollBack();
                  return new JsonResponse('error!');
              }
          }
      }
  </code></pre>