Pergunta

I am using: node-mysql

arrtickers got around 200 values.

arrtickers closure pass in values into sym.

sym will pass in value to each function (running in async mode, each fn will start running on its own without waiting for the previous fn to complete)

problem here is mysql seems unable to handle multiple calls?

events.js:48
        throw arguments[1]; // Unhandled 'error' event
                       ^
Error: reconnection attempt failed before connection was fully set up
    at Socket.<anonymous> (/home/ubuntu/node/node_modules/mysql/lib/client.js:67:28)
    at Socket.emit (events.js:64:17)
    at TCP.onread (net.js:398:51)


        arrtickers.forEach(function(value) {

            var sym= value;


            (function(sym) {

                    url2= "http://test2.com/quote.ashx?t="+sym+"&ty=c&ta=1&p=d&b=1";

                    request({ uri:url2 }, function (error, response, body) {

                      jsdom.env({
                        html: body,
                        scripts: [
                          jqlib
                        ]
                      }, function (err, window) {

                        var $ = window.jQuery;
                        var data= $('body').html();
                        //some scrapping

                        var client2 = mysql.createClient({
                          user: 'root',
                          password: '123',
                            host: '127.0.0.1',
                            port: '3306'
                        });

                        client2.query('USE testtable');

                        sql= "update tbla SET a='"+a+"', b='"+b+"', c='"+c+"', d='"+d+"' where ticker='"+sym+"'";

                        client2.query(  
                             sql, function(err, info){

                                if (err) {  
                                  throw err;  
                                }  
                            }
                        );

                        client2.end();


                      });
                    });

            })(sym);



            (function(sym) {

                    url= "http://test3.com/quote.ashx?t="+sym+"&ty=c&ta=1&p=d&b=1";

                    request({ uri:url3 }, function (error, response, body) {

                      jsdom.env({
                        html: body,
                        scripts: [
                          jqlib
                        ]
                      }, function (err, window) {

                        var $ = window.jQuery;
                        var data= $('body').html();
                        //some scrapping

                        var client3 = mysql.createClient({
                          user: 'root',
                          password: '123',
                            host: '127.0.0.1',
                            port: '3306'
                        });

                        client3.query('USE testtable');

                        sql= "update tbla SET a='"+a+"', b='"+b+"', c='"+c+"', d='"+d+"' where ticker='"+sym+"'";

                        client3.query(  
                             sql, function(err, info){

                                if (err) {  
                                  throw err;  
                                }  
                            }
                        );

                        client3.end();


                      });
                    });

            })(sym);






            (function(sym) {

                    url= "http://test4.com/quote.ashx?t="+sym+"&ty=c&ta=1&p=d&b=1";

                    request({ uri:url4 }, function (error, response, body) {

                      jsdom.env({
                        html: body,
                        scripts: [
                          jqlib
                        ]
                      }, function (err, window) {

                        var $ = window.jQuery;
                        var data= $('body').html();
                        //some scrapping

                        var client4 = mysql.createClient({
                          user: 'root',
                          password: '123',
                            host: '127.0.0.1',
                            port: '3306'
                        });

                        client4.query('USE testtable');

                        sql= "update tbla SET a='"+a+"', b='"+b+"', c='"+c+"', d='"+d+"' where ticker='"+sym+"'";

                        client4.query(  
                             sql, function(err, info){

                                if (err) {  
                                  throw err;  
                                }  
                            }
                        );

                        client4.end();


                      });
                    });

            })(sym);



            //same function repeat for test5.com, test6.com, test7.com, test8.com, test9.com



        });

Below is portion of the code from client.js (part of node-mysql) I don't really understand how the whole process get linked together, any idea guys?

Client.prototype._connect = function() {
  this.destroy();

  var socket = this._socket = new Socket();
  var parser = this._parser = new Parser();
  var self = this;

  socket
    .on('error', this._connectionErrorHandler())
    .on('data', parser.write.bind(parser))
    .on('end', function() {
      if (self.ending) {
        // @todo destroy()?
        self.connected = false;
        self.ending = false;

        if (self._queue.length) {
          self._connect();
        }

        return;
      }

      if (!self.connected) {
        this.emit('error', new Error('reconnection attempt failed before connection was fully set up'));
        return;
      }

      self._connect();
    })
    .connect(this.port, this.host);

  parser.on('packet', this._handlePacket.bind(this));
};
Foi útil?

Solução

You can use node-mysql-queues module, which is a wrapper for the current module you are using. Here is the information: https://github.com/bminer/node-mysql-queues

Sample Multi-Query from their page:

var q = client.createQueue();
q.query(...); 
q.query(...);
q.execute();

client.query(...); //Will not execute until all queued queries (and their callbacks) completed.
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top