Recommendation for Global Connection in Web Server
See original GitHub issueIs 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:
- Created 8 years ago
- Comments:6 (2 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

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
routes/set1.js
UPDATE: This code has been tested.
Absolutely the first way.