Skip to content

Swapping from Pool to PoolCluster #1000

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

Closed
adrianblynch opened this issue Feb 19, 2015 · 3 comments
Closed

Swapping from Pool to PoolCluster #1000

adrianblynch opened this issue Feb 19, 2015 · 3 comments
Assignees
Labels

Comments

@adrianblynch
Copy link

We're currently using the module like so:

// db.js
var mysql = require("mysql");
var config = require('./config')();
var pool = mysql.createPool(config.database.main);
exports.conn = pool;

Then in our application code:

var db = require('./db');

db.query('SELECT * FROM user', function(err, results) {
    // 
});

We now wish to start using PoolCluster, changing db.js to:

var mysql = require("mysql");
var config = require('./config')();
var poolCluster = mysql.createPoolCluster();

poolCluster.add('db01', config.database.db01);
poolCluster.add('db02', config.database.db02);
poolCluster.add('db03', config.database.db03);

exports.conn = poolCluster;

PoolCluster has no method query(). We'd like to leave the application code the same but we're unsure of how to export the pool cluster.

Any thoughts on how we might do that?

@dougwilson
Copy link
Member

It looks like it's a feature that could be added, but if you're in a hurry, you could probably do the following:

poolCluster.query = function query(sql, values, cb) {
  var query = mysql.createQuery(sql, values, cb);

  this.getConnection(function onConnection(err, conn) {
    if (err) {
      query.on('error', function () {});
      query.end(err);
      return;
    }

    // Release connection based off event
    query.once('end', function() {
      conn.release();
    });

    conn.query(query);
  });

  return query;
};

If we added a PoolCluster.prototype.query, that would be very close to what it would be :)

@dougwilson
Copy link
Member

Now that I have learned a little more about the PoolCluster, what you what if definitely possible (sorry it took me so long to realize this):

var mysql = require("mysql");
var config = require('./config')();
var poolCluster = mysql.createPoolCluster();

poolCluster.add('db01', config.database.db01);
poolCluster.add('db02', config.database.db02);
poolCluster.add('db03', config.database.db03);

exports.conn = poolCluster.of('*');

@dougwilson dougwilson self-assigned this Mar 9, 2015
@bjshijh
Copy link

bjshijh commented Apr 8, 2016

I don't think it (means exports.conn = poolCluster.of('*'); ) works.

dveeden pushed a commit to dveeden/mysql that referenced this issue Jan 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

3 participants