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.

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

var connection = new sql.Connection(config); should read var connection = new sql.ConnectionPool(config);. You'll get an error otherwise.
Yes, my answer is dated and now incorrect. When I get the chance I'll be sure to update it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.