1

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.

Mandar Pandit
  • 2,171
  • 5
  • 36
  • 58

1 Answers1

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