title: Multiple concurrent connections in node to mssql
body: <p>I'm using node with express and mssql and querying the database works fine.  However, if I pass 2 concurrent requests at exactly the same time, I get:</p>
  
  <p>UnhandledPromiseRejectionWarning: ConnectionError: Database is already connected! Call close before connecting to different database.</p>
  
  <pre><code>var express = require('express'); // Web Framework
  var app = express();
  var sql = require('mssql/msnodesqlv8');
  
  
  
      const pool = new sql.ConnectionPool({
          database: 'db123',
          server: 'server1',
          driver: 'msnodesqlv8',
          options: {
              trustedConnection: true
          },
      })
  
      var conn = pool;
  
  
  var server = app.listen(5001, function () {
      var host = server.address().address
      var port = server.address().port
      console.log("app listening at http://%s:%s", host, port)
  });
  
  
  
  // GET: SQL Stored procedure return time codes
  app.get('/codes/:userid/:showclosed', function (req, res) {
      conn.connect().then(function () {
          var request = new sql.Request(conn);
          request.input('userid', req.params.userid);
          request.input('showclosed', parseInt(req.params.showclosed));
          request.execute('sel_new', function(err, recordsets, returnValue, affected) {
              if(err) console.log(err);
              res.end(JSON.stringify(recordsets)); // Result in JSON format
              conn.close();
          })
      })
      })
  </code></pre>
  
  <p>EDIT:</p>
  
  <p>Here is what I ended up with:</p>
  
  <pre><code>app.get('/codes/:userid/:showclosed', function (req, res) {
      //const pool1 = new sql.ConnectionPool(config);
     (async function () {
      try {
          let pool = await conn;
          // Stored procedure       
          let result2 = await pool.request()
              .input('userid', req.params.userid)
              .input('showclosed', parseInt(req.params.showclosed))
              .execute('StoredProcedure1', function(err, recordsets, returnValue, affected) {
                  if(err) console.log(err);
                  res.end(JSON.stringify(recordsets)); // Result in JSON format
                  //sql.close();
              })
      } catch (err) {
          console.log(err);
      }
  })()
  
  sql.on('error', err =&gt; {
      console.log(err);
  })
  })
  </code></pre>