Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connection doesn't update status. #80

Open
manhha1006 opened this issue Mar 19, 2015 · 1 comment
Open

Connection doesn't update status. #80

manhha1006 opened this issue Mar 19, 2015 · 1 comment

Comments

@manhha1006
Copy link

I have validate function in Pool to check connection lost


    myPool.prototype.validate  = function(connection){
      return connection["connected"]
    }
    var connected  = pool.validate(conn) // true    
    // lost connect to db (lost network, db crashed ...) 
    // but connection doesn't update connected.
    connected = pool.validate(conn) // true.
   // so, how to know connection lost

@manhha1006 manhha1006 changed the title Connection doesn't update. Connection doesn't update status. Mar 19, 2015
@wankdanker
Copy link
Collaborator

Internally we keep track of "connected" based on if a connection has been successfully opened or closed. It is really never updated asynchronously if a plug is pulled. See this related SO question:

http://stackoverflow.com/questions/3438167/why-isnt-odbcconnection-state-reliable

My suggestion would be to actually do a simple query in the validate function to test if the server is still up:

myPool.prototype.validate  = function(connection){
  try {
    connection.querySync("select 1+1 as test");
  }
  catch (error) {
    //TODO: check the error is really because server failure
    return false;
  }

  return true;
}

Note that that is blocking because of the call to querySync. If there is an asynchronous version of the validate function, I would do this:

myPool.prototype.validate  = function(connection, cb){
  connection.querySync("select 1+1 as test", function (error, result) {
      if (error) {
        //TODO: check the error is really because server failure
        return cb(false);
      }
      return cb(true);
  });
}

This makes me realize that I am doing the same exact unhelpful thing in my odbc-pool module:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants