Skip to content

Instantly share code, notes, and snippets.

Created July 19, 2012 02:24
Show Gist options
  • Save anonymous/3140361 to your computer and use it in GitHub Desktop.
Save anonymous/3140361 to your computer and use it in GitHub Desktop.
MySql Connection pool & Auto reconnection
/*
use ver 2
provide:
connection pool
auto reconnect
Usage:
MySql = require 'lib/mysql' This one
mysql = new MySql(Config)
mysql.getConnection().query ...
Config:
host
port
socketPath
user
password
database
charset
typeCast
pool: pool size > 0
*/
(function() {
var MySql, allDbs, getDb, helpers, mysql, verbose;
verbose = 2;
mysql = require('mysql');
allDbs = {};
MySql = (function() {
function MySql(cfg) {
this.cfg = cfg != null ? cfg : {};
this.cs = [];
this.next = 0;
this._init();
allDbs[this.cfg] = this;
}
MySql.prototype._init = function() {
var i, self, _results;
self = this;
this._max = this.cfg.pool || 3;
delete this.cfg.pool;
if (this._max < 1) {
this._max = 1;
}
i = 0;
_results = [];
while (i < this._max) {
this._conn(i);
_results.push(i++);
}
return _results;
};
MySql.prototype._conn = function(i, cb) {
var c, self;
self = this;
self.cs[i] = c = mysql.createConnection(self.cfg);
console.log("Connecting Mysql " + i);
return c.connect(function(err) {
if (err) {
console.error(err);
self.cs[i].hasError = err;
if (cb) {
cb(null);
}
return null;
}
self.cs[i].on('close', function(err2) {
console.log("Mysql Connection Closed " + err2);
if (err2) {
console.log(err2);
self.cs[i].removeAllListeners();
self.cs[i] = null;
return setTimeout ((function() { return self._conn(i);}),3000);
}
});
self.cs[i].on('error', function(err3) {
console.log("Mysql " + i + " Error " + err3);
if (err3.toString().indexOf('lost')) {
console.log(err3);
self.cs[i].removeAllListeners();
self.cs[i] = null;
return setTimeout ((function() { return self._conn(i);}),3000);
} else {
return self.cs[i].hasError = err3;
}
});
if (verbose) {
console.log("MySql Connection " + i);
}
if (cb) {
return cb(self.cs[i]);
}
});
};
MySql.prototype._checkError = function(i, cb) {
if (this.cs[i].hasError) {
console.log("Reconnect mysql " + i);
this.cs[i].removeAllListeners();
this.cs[i] = null;
return this._conn(i, cb);
} else {
return cb(this.cs[i]);
}
};
MySql.prototype.getConnection = function(cb) {
if (this.next >= this._max) {
this.next = 0;
}
if (verbose > 1) {
console.log("MySql pool instance " + this.next);
}
if (cb) {
return this._checkError(this.next++, cb);
} else {
return this.cs[this.next++];
}
};
MySql.prototype.query = function() {
var arg;
arg = arguments;
return this.getConnection(function(cs) {
if (verbose > 1) {
console.log(arg[0]);
console.log(arg[1]);
}
return cs.query.apply(cs, arg);
});
};
MySql.prototype.format = function(sql, vals, cb) {
return this.getConnection(function(cs) {
var s;
s = cs.format(sql, vals);
return cb(s);
});
};
return MySql;
})();
module.exports.MySql = MySql;
module.exports.getDb = getDb = function(cfg) {
if (allDbs[cfg]) {
return allDbs[cfg];
} else {
return new MySql(cfg);
}
};
}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment