Friday 9 June 2017

Node MySQL Pool Cluster Event Emitter Not Working

The documentation gives examples of registering connection event emitters for pools:

pool.on('connection', function (connection) {
    connection.query('SET SESSION auto_increment_increment=1');
});

Documentation shows how to get a pool from a cluster:

var pool = poolCluster.of('SLAVE*', 'RANDOM');
pool.getConnection(function (err, connection) {});
pool.getConnection(function (err, connection) {});
pool.query(function (error, results, fields) {});

However, var pool = poolCluster.of('SLAVE*', 'RANDOM'); followed by pool.on( ... ) errors with pool.on is not a function

Trying to register the .on('connection') via the cluster executes without error but has no effect.

Code to reproduce:

var mysql = require('mysql');

var mysql_pool_cluster = mysql.createPoolCluster();
mysql_pool_cluster.add('myPool', {user: 'root', password: 'password'});
mysql_pool_cluster.on('connection', function(new_conn) {
    console.log('hello from cluster event emitter');
    new_conn.release();
});

var pool = mysql_pool_cluster.of('myPool', 'ORDER');
try {
    pool.on('connection', function(new_conn) {
        console.log('hello from pool event emitter');
        new_conn.release();
    });
} catch(err) {
    console.error(err);
}

console.log('');

pool.getConnection(function(err, conn) {
    if (err) {
        console.error(err);
    } else {
        console.log('hello from new connection');
        conn.release();

        mysql_pool_cluster.end(function(err) {
            if (err) {
                console.error(err);
            }
        });
    }
});

Output from above code:

TypeError: pool.on is not a function
    at Object.<anonymous> (E:\scratch\scratch_server.js:14:7)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:394:7)
    at startup (bootstrap_node.js:149:9)
    at bootstrap_node.js:509:3

hello from new connection

As you can see, pool.on('connection') fails to execute, and cluster.on('connection') does not emit when running for the first time .getConnection().



via user2426679

No comments:

Post a Comment