diff --git a/src/database.js b/src/database.js index c52c5916b..94fbfe8e5 100644 --- a/src/database.js +++ b/src/database.js @@ -56,6 +56,10 @@ function initialize(callback) { }); gConnectionPool.on('connection', function (connection) { + // connection objects are re-used. so we have to attach to the event here (once) to prevent crash + // note the pool also has an 'acquire' event but that is called whenever we do a getConnection() + connection.on('error', (error) => debug(`Connection ${connection.threadId} error: ${error.message}`)); + connection.query('USE ' + gDatabase.name); connection.query('SET SESSION sql_mode = \'strict_all_tables\''); }); @@ -145,7 +149,6 @@ function rollback(connection, transactionError, callback) { connection.rollback(function (error) { if (error) debug('rollback: error when rolling back', error); - connection.removeAllListeners('error'); connection.release(); callback(transactionError); @@ -161,8 +164,6 @@ function transaction(queries, callback) { beginTransaction(function (error, connection) { if (error) return callback(error); - connection.on('error', callback); - async.mapSeries(queries, function iterator(query, done) { connection.query(query.query, query.args, done); }, function seriesDone(error, results) { @@ -171,7 +172,6 @@ function transaction(queries, callback) { connection.commit(function (error) { if (error) return rollback(connection, error, callback); - connection.removeAllListeners('error'); connection.release(); callback(null, results);