Skip to content

Instantly share code, notes, and snippets.

@ngot
Created October 21, 2013 04:43
Show Gist options
  • Save ngot/7078757 to your computer and use it in GitHub Desktop.
Save ngot/7078757 to your computer and use it in GitHub Desktop.
dbUtils.js
var mysql = require('mysql')
var env = process.env.NODE_ENV || 'development'
var config = require('../../config/config')[env]
var async = require('async');
var kit = require("./kit");
var logger = kit.getLogger();
//create mysql connection
var connection = mysql.createConnection(config.mysql);
connection.connect();
connection.on('error', function (error) {
logger.info('!!!mysql connection error!!!',error,"LOG AT:"+__filename+":"+__line);
setTimeout(function () {
logger.info('!!!going to reConnect!!!',error,"LOG AT:"+__filename+":"+__line);
connection.connect();
}, 1000)
})
//keep mysql connection
var keepConnection = function () {
setInterval(function () {
connection.query('select 1', function (error) {
if (error) {
logger.info('!!!keep connection error!!!',error,"LOG AT:"+__filename+":"+__line);
setTimeout(function () {
connection.connect();
}, 1000)
}
logger.info('!keep connection !',"LOG AT:"+__filename+":"+__line);
})
// }, 10 * 60 * 1000)//10 min
}, 60 * 500)//1 min
}
keepConnection();
exports.connection = connection;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment