App crash on connection loss even when using pool

1,208 views
Skip to first unread message

Jeremiah Cohick

unread,
May 20, 2014, 4:11:12 AM5/20/14
to node-...@googlegroups.com

My app is crashing regularly with this error:

Error: Connection lost: The server closed the connection.
    at Protocol.end (/app/node_modules/mysql/lib/protocol/Protocol.js:78:13)
    at Socket.<anonymous> (/app/node_modules/mysql/lib/Connection.js:78:28)
    at Socket.EventEmitter.emit (events.js:117:20)
    at _stream_readable.js:919:16
    at /app/node_modules/newrelic/node_modules/continuation-local-storage/node_modules/async-listener/glue.js:177:31
    at process._tickCallback (node.js:419:13)


I'm creating a MySQL pool that I export (in a module called 'mysql-connection') for use in my models, like this:

var config = require('../config'),
    mysql = require('mysql');

// Connect to database
var pool = mysql.createPool({
    canRetry: true,
    database: config.db.database,
    host: config.db.host,
    user: config.db.user,
    password: config.db.password,
    port: config.db.port,
    connectionLimit: 9,
    waitForConnections: true,
    queueLimit: 0
});

module.exports = pool;


And then I use this module like this in my models:

var db = require('../lib/mysql-connection');

function getById(id, cb) {
    db.query(
        'SELECT * FROM users WHERE id = ? LIMIT 1',
        [id],
        function(err, users) {
            if (err) {
                return cb(err, null);
            }

            var user = (users.length > 0)? users[0] : null;
            cb(err, user);
        }
    );
}


I thought the pool would automatically reconnect to my MySQL database if the connection closed for any reason. What am I missing?

I'm using Heroku and ClearDB, if that matters. ClearDB will close a connection if it's not used for 90 seconds.

Thanks in advance for any help. :)

Ryan Lee

unread,
May 23, 2014, 8:20:01 AM5/23/14
to node-...@googlegroups.com
Are you getting a new connection and destroying it every time? You might be reusing a timed-out connection.

Very bottom of the pooling docs, talks about releasing versus destroying a connection.


--
You received this message because you are subscribed to the Google Groups "node-mysql" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-mysql+...@googlegroups.com.
To post to this group, send email to node-...@googlegroups.com.
Visit this group at http://groups.google.com/group/node-mysql.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages