Skip to content
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

Issue with multiple orm's and closed connections #86

Closed
dxg opened this issue Mar 21, 2013 · 7 comments
Closed

Issue with multiple orm's and closed connections #86

dxg opened this issue Mar 21, 2013 · 7 comments

Comments

@dxg
Copy link
Collaborator

dxg commented Mar 21, 2013

Hey,

I've hit a rather confusing issue with multiple database connections:

var orm = require('orm');

function getConnection(cb) {
  orm.connect("...", function (err, db) {
    if(err) throw err;

    db.define("animal", {
      name : String
    });

    cb(null, db);
  });
}

getConnection(function(err, db) {
  db.models.animal.drop(function(err) {
    if(err) throw err;

    db.models.animal.sync(function(err) {
      if(err) throw err;
      db.models.animal.create([{name: 'Jerry'}], function(err, animals) {
        if(err) throw err;

        console.log("animals:", animals.length);

        db.models.animal.get(1, function(err, animal) {
          if(err) throw err;
          console.log(animal.name, "connection 1");

          db.close();

          getConnection(function(err, db2) {
            db2.models.animal.get(1, function(err, animal) {
              if(err) throw err;
              console.log(animal.name, "connection 2?");

              animal.name = 'Jane';
              animal.save(function(err, item) {
                if(err) throw err; // <--- Error thrown here
                console.log('saved!'); // <--- Never get this far
                db2.close();
              });
            });
          });
        });
      });
    });
  });
});

It seems that despite me making two connections, ORM is referencing the first one inside both getConnection calls.

Because of this, an error is thrown, along the lines of Error: This socket is closed. or the mysql flavour: Error: Cannot enqueue Query after invoking quit.

Am I doing something wrong?
I spent a long time trying to find the cause, but haven't gotten anywhere.

I've even tried having two differently named getConnection functions, with one connecting to postgres and the other mysql, but I got the same error.

Thanks.

@dresende
Copy link
Owner

I'm going to try your example, I've seen people reporting this regarding node-mysql.

@dresende
Copy link
Owner

You just tested this with mysql right?

@dresende
Copy link
Owner

Oh.. I just saw the same error you posted in postgres.. weird..

@dresende
Copy link
Owner

Gee... stupid bug. It's so stupid you're going to laugh. This is not driver related, it's just cache which is used across drivers. The key to identify a cache item is just the model and id. Since both connections have the same model/id, when you fetch the second one you get the instance cached from the first one (with the first driver - which is then closed).

Doing this solves it temporarily:

db.define("animal", {
    name : String
}, { cache: false });

Do you have a real use case? I was thinking about changing drivers to have an UID for each instance and have the Cache use this for it's keys.

@dxg
Copy link
Collaborator Author

dxg commented Mar 21, 2013

I had this issue with both mysql and postgres.

I extracted the sample above from my app, which uses node-pool, with a pool of ORMs.
It feels like a slightly odd setup, but I needed a pool, and the one in postgresaxomic uses the pool in 'pg' which in turn is dodgy (leaks connections) and has been deprecated in the latest versions of pg. They have new pooling functionality which requires you to manually release connections back into the pool.

To make the new pool work, I would have had to build pool management into ORM, which sounded like a lot of work (but I do think this is a worthy endeavour, to be undertaken some day).
So.. I ended up with a pool of ORM's, which worked fine, until node-pool destroyed idle connections.

Driver UIDs sound like a good fix!

Edit hahaha... yeah, the bug did make me laugh
Edit2 cache: false does indeed fix it.

@dresende
Copy link
Owner

It works fine now :)

@dxg
Copy link
Collaborator Author

dxg commented Mar 22, 2013

Thanks!!

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

No branches or pull requests

2 participants