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

TypeError when initialize multiple connections to the same db with pool option #469

Closed
kbackowski opened this issue Mar 12, 2014 · 2 comments
Milestone

Comments

@kbackowski
Copy link

Hi,

I'm having the following code which throws error

var _ = require('underscore');
var orm = require('orm');

var db = {
  host: '127.0.0.1',
  user: '****',
  password: '****',
  database: 'my_db',
  charset: 'utf8',
  protocol: 'postgres',
  query: {
    pool: true
  }
};

orm.connect(db, function(err, db) {
  var User = db.define('users', {}, {cache: false});
  User.count({}, function(err, results) { console.log(results);});
});

orm.connect(db, function(err, db) {
  var User = db.define('users', {}, {cache: false});
  User.count({}, function(err, results) { console.log(results);});
});
/var/proj/pb-dev3/synchronizer_new/node_modules/orm/lib/Drivers/DML/postgres.js:93
                        this.db.query(query, function (err, result) {
                                ^
TypeError: Object [object Object] has no method 'query'
  at Driver.switchableFunctions.client.execSimpleQuery (/var/proj/pb-dev3/synchronizer_new/node_modules/orm/lib/Drivers/DML/postgres.js:93:12)
  at Driver.count (/var/proj/pb-dev3/synchronizer_new/node_modules/orm/lib/Drivers/DML/postgres.js:202:7)
  at Function.Model.model.count (/var/proj/pb-dev3/synchronizer_new/node_modules/orm/lib/Model.js:467:15)
  at orm.connect.db.define.cache (/var/proj/pb-dev3/synchronizer_new/example.coffee:15:8)
  at /var/proj/pb-dev3/synchronizer_new/node_modules/orm/lib/ORM.js:127:13
  at /var/proj/pb-dev3/synchronizer_new/node_modules/orm/lib/Drivers/DML/postgres.js:57:5
  at /var/proj/pb-dev3/synchronizer_new/node_modules/pg/lib/pool.js:55:9
  at /var/proj/pb-dev3/synchronizer_new/node_modules/pg/node_modules/generic-pool/lib/generic-pool.js:278:11
  at /var/proj/pb-dev3/synchronizer_new/node_modules/pg/lib/pool.js:36:18
  at [object Object].<anonymous> (/var/proj/pb-dev3/synchronizer_new/node_modules/pg/lib/client.js:134:7)
  at [object Object].g (events.js:175:14)
  at [object Object].EventEmitter.emit (events.js:117:20)
  at [object Object].<anonymous> (/var/proj/pb-dev3/synchronizer_new/node_modules/pg/lib/connection.js:97:12)
  at Socket.EventEmitter.emit (events.js:95:17)
  at Socket.<anonymous> (_stream_readable.js:736:14)
  at Socket.EventEmitter.emit (events.js:92:17)
  at emitReadable_ (_stream_readable.js:408:10)
  at emitReadable (_stream_readable.js:404:5)
  at readableAddChunk (_stream_readable.js:165:9)
  at Socket.Readable.push (_stream_readable.js:127:10)
  at TCP.onread (net.js:526:21)

This error only appears when setting pool: true option, without it works perfectly fine. I'm trying to build a multi tenacy solution and the problem occurs when initializing first connection for database (i.e. multiple requests which were started at the same time).

What can be an issue in here ? I would like to use pool option for my database connections.

@dxg
Copy link
Collaborator

dxg commented Mar 13, 2014

Hi,

The problem is that ORM modifies your connection settings - it deletes pool: true during the first orm.connect call.

You can work around this by using something like lodash _.cloneDeep() on the connection settings before passing them in. In the meantime, I'll try get this fixed before the next release.

@dxg dxg added this to the v2.1.4 milestone Mar 13, 2014
@dxg dxg closed this as completed in 23abc6b Mar 13, 2014
@kbackowski
Copy link
Author

It works! :) Thanks for the solution.

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