Skip to content

Issue with multiple orm's and closed connections #86

@dxg

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.

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