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. :)