tediousjs/tedious-connection-pool

get connection Status, pool.acquire method is the only way?

aeolusheath opened this issue · 4 comments

Hi pekim, I create a connection pool as your steps

But I wanna know the connection is valid or not when I create connection.pool .

now after I invoke the pool.acquire method ,in the callback I get the result that connection is invalid.

what should I do ?

Please post an example.

 var poolConfig={};//connectinoPoolConfig
 var connectionConfig={};//connectionConfig
 var pool = new ConnectionPool(poolConfig, connectionConfig);
 // I set the wrong ip address.   when create connectionPool ,it will not throw any error.

 //and when acquire the connection as blow it will throw connection error
 pool.acquire(function (err, connection) {
                        if(err){
                             //get the error
                             console.log(err);
                        }else{
                            done(null);
                        }
                    });

@ben-page

This is the JavaScript and Node.js way of doing things. There are no threads, so blocking actions are executed asynchronously. The connection pool and underlying Tedious connections follow these principles. At the point you call acquire(), the pool hasn't even started creating connections.

You need to understand how asynchronous callback work. Basically, the function passed to acquire is not executed immediately. It's executed once a connection is successfully created or the acquire timeout is exceeded. And you aren't catching the error, where you think you are. If you are receiving any error there, it's not the connection error but rather the Acquire Timeout Exceeded error. To receive the connection error, you need to be listening to the error event.

Check out this example:

var poolConfig = {
    acquireTimeout: 5000 //sorten the timeout to make the example easier to understand
};

var connectionConfig = {
    userName: 'test',
    password: 'test',
    server: 'bad-server-name',
    options: {
        appName: 'pool-test',
        database: 'test'
    }
};

//create the pool
var pool = new ConnectionPool(poolConfig, connectionConfig);

pool.on('error', function(err) {
    console.error('Error Event: ' + err.message);
});

//acquire a connection
pool.acquire(function (err, connection) {
    if (err)
        console.error('Acquire Error: ' + err.message);
});

console.log('done!');

Here's the output:

done!
Error Event: Failed to connect to bad-server-name:1433 - getaddrinfo ENOTFOUND bad-server-name bad-server-name:1433
Error Event: Failed to connect to bad-server-name:1433 - getaddrinfo ENOTFOUND bad-server-name bad-server-name:1433
Error Event: Failed to connect to bad-server-name:1433 - getaddrinfo ENOTFOUND bad-server-name bad-server-name:1433
Error Event: Failed to connect to bad-server-name:1433 - getaddrinfo ENOTFOUND bad-server-name bad-server-name:1433
Error Event: Failed to connect to bad-server-name:1433 - getaddrinfo ENOTFOUND bad-server-name bad-server-name:1433
Error Event: Failed to connect to bad-server-name:1433 - getaddrinfo ENOTFOUND bad-server-name bad-server-name:1433
Error Event: Failed to connect to bad-server-name:1433 - getaddrinfo ENOTFOUND bad-server-name bad-server-name:1433
Error Event: Failed to connect to bad-server-name:1433 - getaddrinfo ENOTFOUND bad-server-name bad-server-name:1433
Acquire Error: Acquire Timeout Exceeded
Error Event: Failed to connect to bad-server-name:1433 - getaddrinfo ENOTFOUND bad-server-name bad-server-name:1433
Error Event: Failed to connect to bad-server-name:1433 - getaddrinfo ENOTFOUND bad-server-name bad-server-name:1433
Error Event: Failed to connect to bad-server-name:1433 - getaddrinfo ENOTFOUND bad-server-name bad-server-name:1433
Error Event: Failed to connect to bad-server-name:1433 - getaddrinfo ENOTFOUND bad-server-name bad-server-name:1433

@ben-page
get it!
thanks very much.