sqlanywhere / node-sqlanywhere

SAP SQL Anywhere Database Client for Node
Apache License 2.0
39 stars 36 forks source link

How to check if the connection is alive? #9

Closed alevinru closed 8 years ago

alevinru commented 8 years ago

We have an app with a pool of db connections. Pooling mechanism would like to check if the connection is still alive every time before acquires it. How can we implement this check?

gperrow-SAP commented 8 years ago

There isn't a specific way to do this, though you could execute a simple "select 1" fetch on the connection, checking for error code -308 "Connection was terminated".

noelhibbard commented 6 years ago

I know this is an old question but there does seem to be an undocumented way to check.

var sqlanywhere = require('sqlanywhere');

var conn = sqlanywhere.createConnection();

var conn_params = {
    Server: 'demo16',
    UserId: 'DBA',
    Password: 'sql'
};

conn.connect(conn_params, function (err) {
    if (err) throw err;
    console.log(conn.connected());
    conn.disconnect();
});

That should output "true" in the console.