问题描述:

I'm trying to build a scheduler with node (v0.10.33) using the node-schedule and node-mysql modules. Code as follows:

var schedule = require('node-schedule');

var mysql = require('mysql');

var rule = new schedule.RecurrenceRule();

rule.second = 59;

schedule.scheduleJob(rule, function(){

var connection = db.connect();

console.log('Successfully established database connection.');

var resultSet = db.select(connection, 'SELECT * from notifications');

db.disconnect(connection);

});

var db = {

connect: function(){

var connection = mysql.createConnection({

host : 'localhost',

user : 'scheduler',

password : 'scheduler'

});

console.log('Trying to establish connection with database...');

connection.connect();

return connection;

},

select: function(connection, queryString){

console.log('Executing database query...');

connection.query(queryString, function(err, rows, fields) {

if (err) throw err;

console.log('The solution is: ', rows[0].messagetext);

return rows;

});

},

disconnect: function(connection){

connection.end();

}

}

When I execute the scheduler I get the following output:

Trying to establish connection with database...

Successfully established database connection.

Executing database query...

/home/user/notificationscheduler/scheduler.js:32

if (err) throw err;

^

Error: connect ECONNREFUSED

at errnoException (net.js:904:11)

at Object.afterConnect [as oncomplete] (net.js:895:19)

--------------------

at Protocol._enqueue (/home/user/notificationscheduler/node_modules/mysql/lib/protocol/Protocol.js:135:48)

at Protocol.handshake (/home/user/notificationscheduler/node_modules/mysql/lib/protocol/Protocol.js:52:41)

at Connection.connect (/home/user/notificationscheduler/node_modules/mysql/lib/Connection.js:119:18)

at Object.db.connect (/home/user/notificationscheduler/scheduler.js:25:14)

at Job.db.connect.mysql.createConnection.host [as job] (/home/user/notificationscheduler/scheduler.js:9:25)

at Job.invoke (/home/user/notificationscheduler/node_modules/node-schedule/lib/schedule.js:117:10)

at null._onTimeout (/home/user/notificationscheduler/node_modules/node-schedule/lib/schedule.js:369:11)

at Timer.listOnTimeout [as ontimeout] (timers.js:112:15)

The mysql server is running locally and I tried connecting and running the same query with the scheduler user successfully.

Has anyone had any experience with these modules and what could be the cause of the problem?

Many thanks

网友答案:

Yes, this is because of asynchronous engine v8. When your call:

db.select(connection, 'SELECT * from notifications')

It won't wait until query is finished and result are returned. This was handling by your callback, but instead of it your are closing the connection with db and query hits an error. You need to close connection in select, if you want to correct behavior:

 select: function(connection, queryString){
            console.log('Executing database query...');
        connection.query(queryString, function(err, rows, fields) {
            if (err) throw err;
            console.log('The solution is: ', rows[0].messagetext);
            connection.end(); // Here close the connection
            return rows;
        });
       },
相关阅读:
Top