Skip to content

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

Closed

Description

@kbackowski

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.

Activity

  1. dxg commented on Mar 13, 2014

    @dxg
    Collaborator

    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.

  2. added this to the v2.1.4 milestone on Mar 13, 2014
  3. kbackowski commented on Mar 13, 2014

    @kbackowski
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions