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.

Mssql cause node.js instance exit after a few queries

See original GitHub issue

Hi,

First, thank you for the job you have done with this it looks great !

But I am having a little issue querying database a few times in a row :

Here is the code

try {

var timeOutCallback = function() {
    var config = {
        "user": "msq2RTFlow",
        "password": "msqService4RTFlow",
        "server": "localhost",
        "database": "MSQ"
    };
    var connection = new mssql.Connection(config);
    connection.connect().then(function() {
        var request = new mssql.Request(connection);
        request.query('SELECT * FROM Patient;').then(function(recordset) {
            console.log(recordset.length);
            connection.close();
            timeoutId = setTimeout(timeOutCallback, 1000);
        }).catch(function(err) {
            console.log(err);
            connection.close();
            timeoutId = setTimeout(timeOutCallback, 1000);
        });
    }).catch(function(err) {
        console.log(err);
        connection.close();
        timeoutId = setTimeout(timeOutCallback, 1000);
    });
};
var timeoutId = setTimeout(timeOutCallback, 1000);

} catch (p_error) {
    console.log(p_error);
}

I took the code to query databasase directly from exemples using promises. Even if I’m not using promises, I am experiencing the same issue. I Am not having neither of my error logs in console, I’m having 7 results from query, ten times, then it stops with no message or error at all.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:12

github_iconTop GitHub Comments

3reactions
tracker1commented, Oct 22, 2015

The following pattern will give you an existing connection/pool when available, as well as support closing, which will kill the handle to the pool, and connection.

IMHO, this should really be the default behavior… why the connection callback doesn’t return the connection is beyond me… also, that there isn’t an internal pool-list is a bit odd too, imho. I’m doing something similar for mssql-ng, which wraps this library with a template processor for parameterized queries.

//getConnection Module
var pool = null;
module.exports = function getConnection() {
  var config = {
    user: "msq2RTFlow",
    password: "msqService4RTFlow",
    server: "localhost",
    database: "MSQ"
  };

  if (pool) return pool;
  var conn = new mssql.Connection(config);

  //override close behavior to eliminate the pool
  var close_conn = conn.close;
  conn.close = function(){
    pool = null;
    close_conn.apply(conn, arguments);
  }

  return pool = conn.connect()
      .then(function(){ return conn; })
      .catch(function(err){
        pool = null;
        return Promise.reject(err);
      });
}
0reactions
willmorgancommented, Oct 18, 2018

Pooling and individual connection lifecycle management has been improved in recent releases so I’m closing this in the hope that upgrading to the latest version resolves the matter.

Read more comments on GitHub >

github_iconTop Results From Across the Web

node-mssql | Microsoft SQL Server client for Node.js
In practice, once the pool is created then there will be no delay for the next connect() call. Also notice that we do...
Read more >
my nodejs script is not exiting on its own after successful ...
I just went through this issue. The problem with just using process.exit() is that the program I am working on was creating handles, ......
Read more >
Top 10 Most Common Node.js Developer Mistakes - Toptal
exports” and “exports” is very important, and is often a cause of frustration among new Node.js developers. Mistake #6: Throwing Errors from Inside...
Read more >
Process | Node.js v19.3.0 Documentation
The Node.js process will exit immediately after calling the 'exit' event listeners causing any additional work still queued in the event loop to...
Read more >
Node.js with MySQL - w3resource
var userId = 'some user provided value'; var sql = 'SELECT * FROM users WHERE id = ' + connection.escape(userId); connection.query(sql, function ...
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