title: Loopback: "Unhandled rejection Error: The transaction is not active" when begin 10 transactions at the same time
body: <p>I have a nodejs project using loopback and postgressql.</p>
  
  <p>I got a function <code>handleTrade</code> like this, simply start the transaction with timeout 30s, query the Trade by id and do some updates:</p>
  
  <pre><code>async function handleTrade(id) {
    const { Trade } = app.models;
    const transaction = await Trade.beginTransaction({
      timeout: 30000 // 30 seconds
    });
  
    try {
      console.log(`${id} - 1`);
      const trade = await Trade.findById(id, { transaction });
      console.log(`${id} - 2`);
  
      if (trade) {
        // Do some updates with the trade
      }
      transaction.commit();
    } catch (e) {
      console.log(`${id} - 3`, e);
      transaction.rollback();
    }
  }
  </code></pre>
  
  <p>I have a list of 10 ids, I want to handle all of them asynchronously and print <code>Done</code> when everything is done.</p>
  
  <pre><code>async function test(ids) {
    const promises = ids.map(id =&gt; handleTrade(id));
    await Promise.all(promises);
    console.log('Done');
    process.exit(0);
  }
  
  test([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
  </code></pre>
  
  <p>And here's the log:</p>
  
  <pre><code>2 - Step 1
  3 - Step 1
  1 - Step 1
  4 - Step 1
  6 - Step 1
  5 - Step 1
  10 - Step 1
  9 - Step 1
  8 - Step 1
  7 - Step 1
  
  -- The process stuck here for 30 seconds (which is the timeout value of the transaction)
  
  2 - Step 2
  Unhandled rejection Error: The transaction is not active: 914ca760-b804-11e8-bb6c-094b32725638
      at process.nextTick (/Users/admin/Work/NIX/nix-project/engine/node_modules/loopback-datasource-juggler/lib/transaction.js:202:12)
      at _combinedTickCallback (internal/process/next_tick.js:131:7)
      at process._tickCallback (internal/process/next_tick.js:180:9)
  
  9 - Step 2
  1 - Step 2
  3 - Step 2
  6 - Step 2
  4 - Step 2
  5 - Step 2
  10 - Step 2
  8 - Step 2
  Unhandled rejection Error: The transaction is not active: 914e5510-b804-11e8-bb6c-094b32725638
      at process.nextTick (/Users/admin/Work/NIX/nix-project/engine/node_modules/loopback-datasource-juggler/lib/transaction.js:202:12)
      at _combinedTickCallback (internal/process/next_tick.js:131:7)
      at process._tickCallback (internal/process/next_tick.js:180:9)
  
  Unhandled rejection Error: The transaction is not active: 914cce71-b804-11e8-bb6c-094b32725638
      at process.nextTick (/Users/admin/Work/NIX/nix-project/engine/node_modules/loopback-datasource-juggler/lib/transaction.js:202:12)
      at _combinedTickCallback (internal/process/next_tick.js:131:7)
      at process._tickCallback (internal/process/next_tick.js:180:9)
  
  Unhandled rejection Error: The transaction is not active: 914cce70-b804-11e8-bb6c-094b32725638
      at process.nextTick (/Users/admin/Work/NIX/nix-project/engine/node_modules/loopback-datasource-juggler/lib/transaction.js:202:12)
      at _combinedTickCallback (internal/process/next_tick.js:131:7)
      at process._tickCallback (internal/process/next_tick.js:180:9)
  
  Unhandled rejection Error: The transaction is not active: 914cf581-b804-11e8-bb6c-094b32725638
      at process.nextTick (/Users/admin/Work/NIX/nix-project/engine/node_modules/loopback-datasource-juggler/lib/transaction.js:202:12)
      at _combinedTickCallback (internal/process/next_tick.js:131:7)
      at process._tickCallback (internal/process/next_tick.js:180:9)
  
  Unhandled rejection Error: The transaction is not active: 914cf580-b804-11e8-bb6c-094b32725638
      at process.nextTick (/Users/admin/Work/NIX/nix-project/engine/node_modules/loopback-datasource-juggler/lib/transaction.js:202:12)
      at _combinedTickCallback (internal/process/next_tick.js:131:7)
      at process._tickCallback (internal/process/next_tick.js:180:9)
  
  Unhandled rejection Error: The transaction is not active: 914cf582-b804-11e8-bb6c-094b32725638
      at process.nextTick (/Users/admin/Work/NIX/nix-project/engine/node_modules/loopback-datasource-juggler/lib/transaction.js:202:12)
      at _combinedTickCallback (internal/process/next_tick.js:131:7)
      at process._tickCallback (internal/process/next_tick.js:180:9)
  
  Unhandled rejection Error: The transaction is not active: 914cf583-b804-11e8-bb6c-094b32725638
      at process.nextTick (/Users/admin/Work/NIX/nix-project/engine/node_modules/loopback-datasource-juggler/lib/transaction.js:202:12)
      at _combinedTickCallback (internal/process/next_tick.js:131:7)
      at process._tickCallback (internal/process/next_tick.js:180:9)
  
  Unhandled rejection Error: The transaction is not active: 914e5511-b804-11e8-bb6c-094b32725638
      at process.nextTick (/Users/admin/Work/NIX/nix-project/engine/node_modules/loopback-datasource-juggler/lib/transaction.js:202:12)
      at _combinedTickCallback (internal/process/next_tick.js:131:7)
      at process._tickCallback (internal/process/next_tick.js:180:9)
  
  7 - Step 2
  Done
  </code></pre>
  
  <p>The processes got stuck for 30 seconds after step 1, then when transaction timeout, it return the Error <code>The transaction is not active</code></p>
  
  <p>And this only happen when I have 10 ids, if I only use 9, it run smoothly</p>
  
  <pre><code>test([1, 2, 3, 4, 5, 6, 7, 8, 9]);
  
  // Logs
  2 - Step 1
  4 - Step 1
  5 - Step 1
  9 - Step 1
  7 - Step 1
  8 - Step 1
  6 - Step 1
  1 - Step 1
  3 - Step 1
  2 - Step 2
  4 - Step 2
  9 - Step 2
  8 - Step 2
  1 - Step 2
  5 - Step 2
  7 - Step 2
  6 - Step 2
  3 - Step 2
  Done
  </code></pre>
  
  <p>If you have any idea please help me. Thanks</p>