David Shields
David Shields

Reputation: 596

Node.js mysql query not executing

We have a simple node.js service running on a standby server to handle high volume polls from an application, to relieve the load on the main apache server.

One of the calls it handles performs a query against the other server, but sometimes does not execute the query. The code is here:

app.get(/^\/rest\/[0-9]*\/*/, function (req, res) {
  ... get controller from url

  if ( controller=='appListEvents'){
      console.log("appListEvents from "+req.connection.remoteAddress+" eid="+eid+" at "+getNow());
      var sql="SELECT * from updates where status='PENDING' and eid = "+eid+" order by stamp, id";
      connection.query(sql, function (error, rows, fields) {
        res.type('application/xml');
        if (error) {
            console.log("appListEvents Query error is "+error);
            res.send('<root><response>AppListEvents Query Error</response></root>');
            return;
        }
        var xml = eventsToXml(rows);
        res.send(xml);
        console.log("appListEvents from "+req.connection.remoteAddress+" eid="+eid+"return XML" + xml);
        return;
    })
    console.log("appListEvents Out of connection, from "+req.connection.remoteAddress+" eid="+eid);
  }

To my mind, the Out of Connection message should never appear - either there is data from the query, in which case some xml is sent back, or there is no data in which case the exentsToXml returns xml with no data in it.

This happens as I just described, but SOMETIMES calls to the service trigger the out of connection message. The error handling in the if(error) part is never triggered.

All I can think is that queries are being dropped because I am overloading the database: how do I protect against whatever is going wrong ?

Upvotes: 0

Views: 334

Answers (1)

E.K
E.K

Reputation: 120

The Out of connection message should appear at any time because the connection.query() callback is executed in a asynchronous way.

Upvotes: 1

Related Questions