You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Issue with multiple orm's and closed connections #86
I've hit a rather confusing issue with multiple database connections:
varorm=require('orm');functiongetConnection(cb){orm.connect("...",function(err,db){if(err)throwerr;db.define("animal",{name : String});cb(null,db);});}getConnection(function(err,db){db.models.animal.drop(function(err){if(err)throwerr;db.models.animal.sync(function(err){if(err)throwerr;db.models.animal.create([{name: 'Jerry'}],function(err,animals){if(err)throwerr;console.log("animals:",animals.length);db.models.animal.get(1,function(err,animal){if(err)throwerr;console.log(animal.name,"connection 1");db.close();getConnection(function(err,db2){db2.models.animal.get(1,function(err,animal){if(err)throwerr;console.log(animal.name,"connection 2?");animal.name='Jane';animal.save(function(err,item){if(err)throwerr;// <--- Error thrown hereconsole.log('saved!');// <--- Never get this fardb2.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.
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).
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 Edit2cache: false does indeed fix it.
Activity
dresende commentedon Mar 21, 2013
I'm going to try your example, I've seen people reporting this regarding node-mysql.
dresende commentedon Mar 21, 2013
You just tested this with mysql right?
dresende commentedon Mar 21, 2013
Oh.. I just saw the same error you posted in postgres.. weird..
dresende commentedon Mar 21, 2013
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:
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 commentedon 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
postgresaxomicuses 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: falsedoes indeed fix it.Reindents package.json, adds hat as new dependency for driver uid gen…
Generates an uid for every driver instance (#86)
Changes singleton uid creation to use driver uid (#86)
dresende commentedon Mar 21, 2013
It works fine now :)
dxg commentedon Mar 22, 2013
Thanks!!