title: Handling rollbacked MySQL transactions in Node.js
body: <p>I'm dealing with a promblem for a couple of days, and I'm really hoping, you could help me.</p>
  
  <p>It's a <code>node.js</code> based API using <code>sequelize</code> for <code>MySQL</code>. </p>
  
  <p>On certain API calls the code starts <code>SQL</code> transactions which lock certain tables, and if I send multiple requests to the API simultaneously, I got <code>LOCK_WAIT_TIMEOUT</code> errors. </p>
  
  <pre><code>var SQLProcess = function () {
      var self = this;
      var _arguments = arguments;
  
      return sequelize.transaction(function (transaction) {
              return doSomething({transaction: transactioin});
          })
          .catch(function (error) {
              if (error &amp;&amp; error.original &amp;&amp; error.original.code === 'ER_LOCK_WAIT_TIMEOUT') {
  
                  return Promise.delay(Math.random() * 1000)
                      .then(function () {
                          return SQLProcess.apply(self, _arguments);
                      });
  
              } else {
                  throw error;
              }
          });
  };
  </code></pre>
  
  <p>My problem is, the simultaneously running requests lock each other for a long time, and my request returns after a long-long time (~60 seconds).</p>
  
  <p>I hope I could explain it clear and understandable, and you could offer me some solution.</p>