I am refering to the node-mssql library for my application in node.js using mssql for database connection. mssql-ref. I have checked for creation pool of connections, but I found the same for mysql and not for mssql. Please give any reference where I can find sample code snippet for mssql connection pool with node.js. Thanks.
Asked
Active
Viewed 8,887 times
1 Answers
2
You would find that information in the page you're referring to yourself. But it's worth noting that the documenting is not explicit about how a Connection pool is created.
var connection = new sql.Connection(config);
The variable connection here is actually a connection pool that still needs to connect...
var connection = new sql.Connection(config, function(err) {
// ... error checks
// Query
var request = new sql.Request(connection);
// or: var request = connection.request();
request.query('select 1 as number', function(err, recordset) {
// ... error checks
console.dir(recordset);
});
});
Or using promises by omitting a callback argument
var connection = new sql.Connection(config);
//connection is actually a connection pool
connection.connect().then(function() {
var request = new sql.Request(connection);
request.query('select * from mytable').then(function(recordset) {
// ...
}).catch(function(err) {
// ...
});
}).catch(function(err) {
// ...
});
After you create a single instance of Connection, let all your requests use it like in the example above as opposed to instantiating a new Connection object for each request.
If your asking in relation to a Express 4 web application, see this question and answers.

Community
- 1
- 1

Christiaan Westerbeek
- 10,619
- 13
- 64
- 89
-
`var connection = new sql.Connection(config);` should read `var connection = new sql.ConnectionPool(config);`. You'll get an error otherwise. – amunnelly Nov 08 '18 at 18:02
-
2Yes, my answer is dated and now incorrect. When I get the chance I'll be sure to update it. – Christiaan Westerbeek Nov 08 '18 at 19:39