question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Recommendation for Global Connection in Web Server

See original GitHub issue

Is it better to create one global connection for the life of a web server, for example:

sql.connect(config, function(err) {
  if (err) { console.log('Connect err: ' + err); return; }
  isConnected = true;
});

exports.getLocations = function(callback) {
  var request = new sql.Request();
  request.query('select * from Locations', function(err, recordset) {
    if (err) { callback(err, null); return; }
    callback(null, recordset);
    return;
  });
}

exports.getInstruments = function(callback) {
  var request = new sql.Request();
  request.query('select top 100 * from InstrumentsTLC', function(err, recordset) {
    if (err) { callback(err, null); return; }
    callback(null, recordset);
    return;
  });
}

Or should each request be wrapped in its own connection, for example:

exports.getLocations = function(callback) {
  sql.connect(config, function(err) {
    if (err) { callback(err, null); return; }

    var request = new sql.Request();
    request.query('select * from Locations', function(err, recordset) {
      if (err) { callback(err, null); return; }
      callback(null, recordset);
      return;
    });
  });
}

exports.getInstruments = function(callback) {
  sql.connect(config, function(err) {
    if (err) { callback(err, null); return; }

    var request = new sql.Request();
    request.query('select top 100 * from InstrumentsTLC', function(err, recordset) {
      if (err) { callback(err, null); return; }
      callback(null, recordset);
      return;
    });
  });
}

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
christiaanwesterbeekcommented, May 20, 2015

As for the code in your first example, your probably have several route.js files and a primairy server.js. In that case, may I suggest the following.

It shows an Express 4 application that uses a single connection pool across several files where route logic is handled.

server.js

//require stuff
var express = require('express');
var sql     = require('mssql');
var config  = {/*...*/};
//instantiate a connection pool
var cp      = new sql.Connection(config); //cp = connection pool
//require route handlers and use the same connection pool everywhere
var set1    = require('./routes/set1')(cp);
var set2    = require('./routes/set2')(cp);

//generic express stuff
var app = express();

//...
app.get('/path', set1.get);
//.post(set2.get);

//connect the pool and start the web server when done
cp.connect().then(function() {
  console.log('Connection pool open for duty');

  var server = app.listen(3000, function () {

    var host = server.address().address;
    var port = server.address().port;

    console.log('Example app listening at http://%s:%s', host, port);

  });
}).catch(function(err) {
  console.error('Error creating connection pool', err);
});

routes/set1.js

var sql     = require('mssql');

module.exports = function(cp) {
  var me = {
    get: function(req, res, next) {
      var request = new sql.Request(cp);
      request.query('select * from test', function(err, recordset) {
        if (err) {
          console.error(err);
          res.status(500).send(err.message);
          return;
        }
        res.status(200).json(recordset);
      });
    }
  };

  return me;
};

UPDATE: This code has been tested.

1reaction
patriksimekcommented, May 20, 2015

Absolutely the first way.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What is connection pooling, and why should you care
Database connections can get expensive at scale. Pooling connections can help, so here's what connection pooling is and how to do it!
Read more >
Should I keep this "GlobalConnection" or create connection for ...
The application has a notion of a "Global Connection" -- that is a single connection that it opens at the start, and then...
Read more >
Global Connection - Loytec
A global connection creates a data cloud with a system-wide name. Data points which are added to a global connection can send values...
Read more >
Best Practices for securing AD FS and Web Application Proxy
The most important security recommendation for your AD FS infrastructure is to ensure you have a means in place to keep your AD...
Read more >
MySQL Connection Handling and Scaling
A long lived connection is a connection that is open “indefinitely”. For example one might have a Web server or an Application server...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found