-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Throw error on connection failed in pool #708
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
Comments
The poll doesn't even make a connection to the database until a |
That makes sense, but then I should get an error that a connection could not be made. But it waits until a timeout is thrown in stead. So, just like This is only when host is an ip (e.g. 1.1.1.1), when host is a name e.g. 'localhostiness' (deliberate typo) I get an error thrown immediately. |
Testcase Connecting to a non-existing host will not throw an error, instead it will wait forever (for timeout). If you fill in correct database credentials you will see that any other mistake will throw an error immediately (once the query is executed). var mysql = require('mysql') // npm install mysql
, q = require('q') // npm install q
, util = require('util')
, queue = []
, clientOpts = {
host : '1.1.1.1',
user : 'user',
password : 'pass',
database : 'mydb'
};
// If you have a correct host in clientOpts but a wrong user/name/db, you'll receive an error immediately.
var pool = mysql.createPool(clientOpts);
pool.on('connection', function(connection) {
util.log('Connected to MySql db');
});
pool.on('error', function(err) {
util.log('Oh shit.');
throw err;
});
exports.query = query;
function query(query, values) {
var link;
return q.nfcall(pool.getConnection.bind(pool))
.then(function(connection) {
link = connection; // scope
return q.nfcall(link.query.bind(link), query, values);
})
.timeout(2000, 'Big fat timeout') // Adding short timeout because the default is a few minutes
.then(function(rows) {
link.release();
return rows;
})
.fail(function (err) {
util.log(err);
throw err;
});
}
// Test the connection
this.query('SELECT 1').then(function(rows) {
util.log(rows);
}).done(); |
The query is not what is timing out, it is the call to var mysql = require('mysql') // npm install mysql
, util = require('util')
, queue = []
, clientOpts = {
host : '1.1.1.1',
user : 'user',
password : 'pass',
database : 'mydb'
};
// If you have a correct host in clientOpts but a wrong user/name/db, you'll receive an error immediately.
var pool = mysql.createPool(clientOpts);
pool.on('connection', function(connection) {
util.log('Connected to MySql db');
});
exports.query = query;
function query(query, values, callback) {
pool.getConnection(function(err,connection) {
if (err) return callback(new Error('connection error: ' + err.message));
connection.query(query, values, function(err,rows){
connection.release();
if (err) return callback(new Error('query error: ' + err.message));
callback(null,rows);
});
});
}
// Test the connection
this.query('SELECT 1', [], function(err,rows) {
if (err) throw err;
util.log(rows);
}); results in
|
I am not sure I am following. Because using proper credentials, the connection and the query are established/queried without a problem.
is the same as
But since your example throws immediately, there must be something wrong on my end. I'm just not seeing the problem. I will experiment with this. |
When you use |
This is true, but one would expect |
OK. Does the code I posted call back as fast as you are looking for? If so, then it seems like your issue would be with your use of the |
I will get back to you on that. 👍 |
Sorry for the delay.
No it does not. The callback never gets executed in your code either. Looks like my initial report was valid. There is no error thrown when you are trying to connect to a non-existing host. So we need to guess based on a lack of response before a certain timeout. |
@Redsandro can you try using the code from PR #726 and specifying var pool = mysql.createPool({
host: '1.1.1.1',
user: 'user',
password: 'pass',
database: 'mydb',
connectTimeout: 2000 // <-- this is new, 2s from your example with Q
}); |
@dougwilson Sorry for the late response, I was on the toilet. Indeed I get the a timeout error when specifying this
Set this to a value higher than 10 seconds and a different hardcoded timeout wins:
It makes sense, because it should not take that long to connect. When I opened this bug I didn't understand why a timeout was needed at all, but it makes sense. We cannot know that a connection was failed if we do not get a reply from the (non-existing) server. If we connect to an existing server with bad credentials, we get I'm satisfied and I forgot to let you know. 👍 So don't change anything on my account. With that said, I do have the feeling that setting |
Hi @Redsandro , the inactivity timeout is not hard-coded in any way. That
These are two independent timeouts and track different aspects of the connection lifecycle. They are additive, because they are separate. The These are separate timeouts because they are very different in nature -- one is sensitive to your networking infrastructure and the other is sensitive to your MySQL application/authentication infrastructure. Typically you want to have a very small I hope this helps! |
Thank you for the elaborate response. I get it now. I have no further comments at this time. 😄 |
When I
var pool = mysql.createPool(options);
to a wronghost
(ip address), I catch nothing inpool.on('error', function(error) {});
.Instead, not until I do a
pool.getConnection()
+connection.query()
and sit out the full (default) timeout of two minutes or something do I receive anETIMEDOUT
error.Is there (or should there be) a way to find out immediately if the connection failed?
In contrast, if I (purposely) specify a wrong username, I immediately catch an
ER_DBACCESS_DENIED_ERROR
error. But a wrong host will just do nothing until the connection times out.The text was updated successfully, but these errors were encountered: