Skip to content

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

Closed
lperrin opened this issue May 14, 2013 · 7 comments
Closed

Doesn't preserve domains #489

lperrin opened this issue May 14, 2013 · 7 comments

Comments

@lperrin
Copy link

lperrin commented May 14, 2013

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:

function preserveDomain(done) {
  var d = process.domain;

  return function () {
    var args = arguments;

    d.run(function () {
      done.apply(null, args);
    });
  };
}

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 ?

@felixge
Copy link
Collaborator

felixge commented May 15, 2013

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 preserveDomains() function works in this regard.

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.

@lperrin
Copy link
Author

lperrin commented May 16, 2013

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 process.domain.id to see in which domain I'm running. The problem is that the value changes after calling mysql. If I get an error later (even not related with mysql), I end up sending a 500 to the wrong connection.

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 :)

@lperrin
Copy link
Author

lperrin commented May 17, 2013

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.

@lperrin lperrin closed this as completed May 17, 2013
@lperrin
Copy link
Author

lperrin commented Sep 5, 2013

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.

@felixge
Copy link
Collaborator

felixge commented Sep 5, 2013

I was wondering if I could quote your comment:

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 ; )

@dougwilson
Copy link
Member

As of version 2.0.1, process.domain will now be preserved in your callback without you needing to do anything special.

@lperrin
Copy link
Author

lperrin commented Jan 17, 2014

I just read the PR. Great work!

2014/1/17 Douglas Christopher Wilson [email protected]

As of version 2.0.1, process.domain will now be preserved in your
callback without you needing to do anything special.


Reply to this email directly or view it on GitHubhttps://github.com//issues/489#issuecomment-32624445
.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants