title: node.js Global connection already exists. Call sql.close() first
body: <p>I'm trying to create web services using node.js from an sql server database,in the frontend when i call those 2 webservices simultaneously it throws an error Global connection already exists. Call sql.close() first .</p>
  
  <p>Any Solution ?  </p>
  
  <pre><code>var express = require('express');
  var router = express.Router();
  var sql = require("mssql");
  
  router.get('/Plant/:server/:user/:password/:database', function(req, res, next) {
  
      user = req.params.user;
      password = req.params.password;
      server = req.params.server;
      database = req.params.database;
  
  
      // config for your database
      var config = {
          user: user,
          password: password,
          server: server,
          database:database
  
      };
  
      sql.connect(config, function (err) {
  
  
  
          // create Request object
          var request = new sql.Request();
  
          // query to the database and get the records
          request.query("SELECT  distinct PlantName FROM MachineryStateTable"
              , function (err, recordset) {
  
                  if (err) console.log(err)
                  else {
  
                      for(i=0;i&lt;recordset.recordsets.length;i++) {
                          res.send(recordset.recordsets[i])
                      }
  
                  }
                 sql.close();
  
              });
  
      });
  
  });
  
  
  
  
  
  router.get('/Dep/:server/:user/:password/:database/:plantname', function(req, res, next) {
  
      user = req.params.user;
      password = req.params.password;
      server = req.params.server;
      database = req.params.database;
      plantname = req.params.plantname;
  
      // config for your database
      var config = {
          user: user,
          password: password,
          server: server,
          database:database
  
      };
  
      sql.connect(config, function (err) {
  
  
  
          // create Request object
          var request = new sql.Request();
  
          // query to the database and get the records
          request.query("SELECT  distinct DepName FROM MachineryStateTable where PlantName= '"+plantname+"'"
              , function (err, recordset) {
  
                  if (err) console.log(err)
                  else {
  
                      for(i=0;i&lt;recordset.recordsets.length;i++) {
                          res.send(recordset.recordsets[i])
  
                      }
                      sql.close();
                  }
  
              });
      });
  });
  
  
  
  module.exports = router;
  </code></pre>