Skip to content

Fix call site when using Pool.prototype.query #715

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

Merged
merged 1 commit into from
Feb 23, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions lib/Pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,23 @@ Pool.prototype.query = function (sql, values, cb) {
cb = values;
values = null;
}


var connection;
var query = Connection.createQuery(sql, values, function (err, rows, fields) {
connection.release();
cb.apply(this, arguments);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the value of this here? Should null or a self-like variable be passed instead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All this did was move the previous

   conn.query(sql, values, function (err, rows, fields) {
     conn.release();
     cb.apply(this, arguments);
   });

The purpose is to cal the callback with the same this that conn.query would have called it with such that pool.query and conn.query have the same this instead of possible different ones. This allows people to use conn.query and pool.query interchangeably.

If you were wondering what this is pointing it, it is point to the connection.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm on a mobile device right now, so please forgive me for not diving deeper into this myself right now. It seems unlikely that "this" points to the connection from the createQuery callback though.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like it is actually a connection - 'this' for this anonymous function is set at https://github.com/felixge/node-mysql/blob/master/lib/protocol/sequences/Sequence.js#L78

});

if (this.config.connectionConfig.trace) {
// Long stack trace support
query._callSite = new Error;
}

this.getConnection(function (err, conn) {
if (err) return cb(err);

conn.query(sql, values, function (err, rows, fields) {
conn.release();
cb.apply(this, arguments);
});
connection = conn;
conn.query(query);
});
};

Expand Down
2 changes: 1 addition & 1 deletion lib/protocol/Protocol.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ Protocol.prototype._enqueue = function(sequence) {

if (this._config.trace) {
// Long stack trace support
sequence._callSite = new Error;
sequence._callSite = sequence._callSite || new Error;
}

this._queue.push(sequence);
Expand Down
19 changes: 19 additions & 0 deletions test/integration/pool/test-long-stack-traces.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
var assert = require('assert');
var common = require('../../common');
var pool = common.createPool({connectionLimit: 1});

var err;
pool.getConnection(function(_err, conn) {
if (_err) throw _err;
pool.query('invalid sql', function(_err) {
err = _err;
pool.end();
});
process.nextTick(function() {
conn.release();
});
});

process.on('exit', function() {
assert.ok(err.stack.indexOf(__filename) > 0);
});