Connection doesn't update status.
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
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: