1

I'm using the npm mysql library. (Pretty new to mysql) I have a script that I'm running that inserts data into a mysql table. However, for some reason some of the connections time out after some insertions. I get this error:

{ Error: connect ETIMEDOUT
at PoolConnection.Connection._handleConnectTimeout (/Users/max/projects/stock-news-angular/node_modules/mysql/lib/Connection.js:425:13)
at Socket.g (events.js:286:16)
at emitNone (events.js:86:13)
at Socket.emit (events.js:185:7)
at Socket._onTimeout (net.js:334:8)
at tryOnTimeout (timers.js:232:11)
at Timer.listOnTimeout (timers.js:202:5)
--------------------
at Protocol._enqueue (/Users/max/projects/stock-news-angular/node_modules/mysql/lib/protocol/Protocol.js:141:48)
at Protocol.handshake (/Users/max/projects/stock-news-angular/node_modules/mysql/lib/protocol/Protocol.js:52:41)
at PoolConnection.connect (/Users/max/projects/stock-news-angular/node_modules/mysql/lib/Connection.js:136:18)
at Pool.getConnection (/Users/max/projects/stock-news-angular/node_modules/mysql/lib/Pool.js:48:16)
at Pool.query (/Users/max/projects/stock-news-angular/node_modules/mysql/lib/Pool.js:200:8)
at Object.<anonymous> (/Users/max/projects/stock-news-angular/app/scraper.js:90:34)
at initialize.exports.each (/Users/max/projects/stock-news-angular/node_modules/cheerio/lib/api/traversing.js:300:24)
at /Users/max/projects/stock-news-angular/app/scraper.js:82:29
at Array.forEach (native)
at /Users/max/projects/stock-news-angular/app/scraper.js:75:21

errorno: 'ETIMEDOUT', code: 'ETIMEDOUT', syscall: 'connect', fatal: true }

When I look at my mysql database, it looks like this:

    .... some data
    +------------+----------------------------------------------------------------------------------------------------------------------------------------------------+-----------------------+---------------------------+
    12131 rows in set (0.09 sec)

So it looks like a bunch of rows are being successfully added, but still many are not added. Here is the code for creating the connection -- I'm using a pooled connection (not sure if that's right), since I thought that i had to do many queries so I would just pool them together. The declaration of the pool:

        var pool = mysql.createPool({
        host: 'localhost',
        user: 'root',
        password: 'secret',
        database: 'stock_news'
    });

And the query:

        pool.query({sql: 'INSERT INTO major_news SET ?', timeout:40000, values: post}, function(err, result) {
        if (err)
           console.log(err);
        });

Perhaps I don't understand mysql well enough but I'm not sure why the connection is timing out after a while. I've already increased the timeout to 40000ms, so I'm thinking it might be something else that I don't see.

needhelpwithR
  • 283
  • 1
  • 4
  • 15

1 Answers1

0

Are you getting the pool connection before doing the pool.query?

var mysql = require('mysql');
var pool  = mysql.createPool({
  host     : 'example.org',
  user     : 'bob',
  password : 'secret',
  database : 'my_db'
});

pool.getConnection(function(err, connection) {
  // connected! (unless `err` is set)
  // queries here, when all queries are finished you do connection.release() to return the connection back to the pool
});

You might need to pass the connection object for another function and do a callback when all the queries are done (to release the connection), since not releasing the connection to the pool will eventually close the connection to the server.