Skip to content

Issue with multiple orm's and closed connections #86

Closed

Description

@dxg

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.

Activity

  1. dresende commented on Mar 21, 2013

    @dresende
    Owner

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

  2. dresende commented on Mar 21, 2013

    @dresende
    Owner

    You just tested this with mysql right?

  3. dresende commented on Mar 21, 2013

    @dresende
    Owner

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

  4. dresende commented on Mar 21, 2013

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

  5. dxg commented on Mar 21, 2013

    @dxg
    CollaboratorAuthor

    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.

  6. dresende commented on Mar 21, 2013

    @dresende
    Owner

    It works fine now :)

  7. dxg commented on Mar 22, 2013

    @dxg
    CollaboratorAuthor

    Thanks!!

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions