-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Doesn't preserve domains #489
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
First of all, I think domains are an abomination and so is exception handling in node.js in general. Unfortunately this is by design, so I'll stop bitching about it. About your issue: Are you trying to isolate each connection into a domain? Or are you trying to isolate all queries made within the context of a given request? The later may indeed be tricky and I'm not sure how your That being said, node-mysql features what I refer to as "callback safety". This means that an exception triggered within a callback made by node-mysql will not cause harm to the state of this connection. So it's not necessary to discard the connection object when detecting such an error. |
I'm trying to isolate each connection in a domain. Basically, I include this middleware at the beginning of my express routing chain: var count = 1;
exports.attach = function (req, res, next) {
var d = domain.create();
d.id = count++;
d.add(req);
d.add(res);
d.run(next);
d.on('error', function (err) {
console.error(req.path + ': ' + err.message);
res.send(500, {message: 'something went horribly wrong'});
});
}; Later, I can always check This problem can happen with most db connectors and I'm surprised I haven't read about it earlier. Also, I'm starting to understand why you think domains are bad :) |
On a second thought, this is really not a problem with node-mysql, so I'm closing this. Thanks for giving me your opinion on this. |
Hello ! Sorry, this is not related to this issue, but I'm giving a talk next week about node.js domains and I was wondering if I could quote your comment: "First of all, I think domains are an abomination and so is exception handling in node.js in general…". Of course, it would be as a joke. I'm speaking to a local node.js user group in Paris. |
Sure, go ahead. I mean it's obviously a bit of a rant, and the truth is more complex, so I wouldn't recommend using the quote to support a deeper argument, but whatever ; ) |
As of version |
I just read the PR. Great work! 2014/1/17 Douglas Christopher Wilson [email protected]
|
TL;DR: I don't think you should change the code, but perhaps you should include a warning in the doc.
I'm using domains in my web app. I followed the sample in the documentation to attach each connection to a domain:
http://nodejs.org/api/domain.html#domain_explicit_binding
Problem is, node-mysql doesn't preserve domains. Because of the sequential protocol, the SQL parser always runs in the same domain and passes it to all callbacks.
The fix for me is quite simple. I just attach the domain in a closure:
However, this was very puzzling and it took me a long time to understand what was going on. I think that the code should stay domain-agnostic, but maybe include a warning in the doc ?
The text was updated successfully, but these errors were encountered: