Skip to content

process.domain become undefined after mysql.query(... #308

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
ajostergaard opened this issue Oct 21, 2012 · 10 comments
Closed

process.domain become undefined after mysql.query(... #308

ajostergaard opened this issue Oct 21, 2012 · 10 comments

Comments

@ajostergaard
Copy link

Experimenting with v8s new process.domain and noticed that my newly created domain becomes 'undefined' if I do a query:

var d = domain.create();
d.run(function() {
  process.domain.data = {};
  console.log(process.domain); //outputs domain data structure
  mysqlClient = mysql.createConnection(...);
  mysqlClient.connect();
  mysqlClient.query(..., function(...) {
     console.log(process.domain); //outputs 'undefined'
  });
});

(Unless of course I'm doing something completely stupid which is of course entirely possible!)

@dresende
Copy link
Collaborator

I tested your example (only without mysqlClient.connect()) and it works fine. What node version are you using?

@ajostergaard
Copy link
Author

That's odd.

I tried without connect and makes no difference.

aj$ node -v
v0.8.8

I'll try to reproduce my issue independently of the rest of my code and update here ...

@dresende
Copy link
Collaborator

Just to clarify, I'm using node v0.8.12, [email protected]

@ajostergaard
Copy link
Author

I was mistaken when I said that .connect made no difference. Actually it works without .connect. :) So the bug is that using the explicit connect method causes subsequent queries to destroy the domain.

@dresende
Copy link
Collaborator

dresende commented Nov 5, 2012

It's odd but... I was re-reading this issue and copied your code with [email protected] and it just works. Could you confirm this?

@ajostergaard
Copy link
Author

I really don't know/understand what was happening but I've stopped using domains as a result - sorry I can't help further and thanks for taking the time to try to assist.

@jbrumwell
Copy link

I'm running into the same problem with [email protected] and mysql@alpha7

console.log(typeof process.domain);
        db.query('SELECT field FROM table WHERE field IN (?)', [ids], function(err, results) {
            console.log(typeof process.domain);
            process.exit();
});

outputs

object
undefined

@dougwilson
Copy link
Member

This is just how domains currently work. Even https://github.com/mranney/node_redis and other modules have this issue. It has to do with using callbacks; you need to use domain.intercept:

var d = domain.create();
d.run(function () {
  client.query('...', d.intercept(function (rows) {
    // ... use rows (note, first arguments error was "intercepted" by the domain)
  }));
});

@jbrumwell
Copy link

yeah I know that domain#run has that issue and I've been able to get around it by using domain#enter and domain#exit at other portions of the code was trying to avoid wrapping the callbacks. Your solution works thanks for your help doug

@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.

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

No branches or pull requests

4 participants