title: NodeJS How To Handle Concurrent Request To MySQL
body: <p>I am having a problem with NodeJS.
  When I am trying to post/update</p>
  
  <ul>
  <li><code>One balance</code></li>
  <li><code>One account</code></li>
  <li>posting from <code>Two different users</code></li>
  <li><code>at the same time.</code></li>
  </ul>
  
  <p>Example: </p>
  
  <p><a href="https://i.stack.imgur.com/ucFfe.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ucFfe.png" alt="example of t_account table"></a></p>
  
  <p>I have API with ExpressJS for post data to the <code>t_account</code> table like this. </p>
  
  <p>I need to get latest balance first from <code>max(id)</code> after each transaction.</p>
  
  <p>I'm trying to get balance of 400 <code>NOT</code> 300</p>
  
  <p>For Example:</p>
  
  <ol>
  <li>Starting Balance = 100  </li>
  <li>1st new post = 100, => balance =200  </li>
  <li>2nd new post = 200, => balance =400</li>
  </ol>
  
  <p><strong>Code Example:</strong></p>
  
  <pre><code>router.post('/saveBalance',async function (req, res) {
  try {
      let SQL="SELECT balance FROM t_account WHERE id=(SELECT max(id) FROM t_account WHERE no='"+req.body.no+"')";
      let queryResult=await db.myexec(SQL);
      let newBalance=queryResult[0].balance+req.body.balance;
      let SQL2="INSERT INTO t_account(no,balance) VALUES('"+req.body.no+"',"+newBalance+")";
      let queryResult2=await db.myexec(SQL2);
      res.status(200).json(  {
          error:"false",
          code:"00",
          message:"Balance Bank is saved",
          })
  } catch (error) {
      res.status(400).json(  {
          error:"true",
          code:"03",
          message:"Balance Bank failed to saved",
          })
   }})
  </code></pre>
  
  <p>The problem is when two user post data in the same time, I get the incorrect balance.</p>
  
  <p>Code Example of Initializing my Transactions:</p>
  
  <blockquote>
  <pre><code>const axios = require('axios');
  axios.post('http://localhost:3000/rbank/saveBalance/', 
   {
      "no":"11",
      "balance":100
    }
   )
  axios.post('http://localhost:3000/rbank/saveBalance/', 
   {
      "no":"11",
      "balance":200
   }
  )
  </code></pre>
  </blockquote>
  
  <p><code>MySQL</code> Console Logs</p>
  
  <p><a href="https://i.stack.imgur.com/Apdf6.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Apdf6.png" alt="enter image description here"></a>
  <a href="https://i.stack.imgur.com/pPqRJ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/pPqRJ.png" alt="enter image description here"></a></p>
  
  <p>The last balance not right showing 300 instead of 400.</p>
  
  <p>What is going wrong here?</p>