Skip to content

Commit

Permalink
fixed reconnection problem
Browse files Browse the repository at this point in the history
Conflicts:
	lib/adapters/sql/mysql.js
  • Loading branch information
eden-lane authored and mde committed Mar 1, 2014
1 parent f745064 commit b657333
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions lib/adapters/sql/mysql.js
Expand Up @@ -39,19 +39,39 @@ utils.mixin(Adapter.prototype, new (function () {

this.COLUMN_NAME_DELIMITER = '`';

this.handleDisconnect = function () {
var self = this;
setTimeout(function() {
self.client = mysql.createConnection(self.config);
self.connect();
}, Math.pow(3, reconnectAttempts) * 1000);
reconnectAttempts++;
};

this.init = function () {
var self = this;
this.client = mysql.createConnection(this.config);
this.client.on('error', function(err) {
if (err.code === 'PROTOCOL_CONNECTION_LOST' && reconnectAttempts < RECONNECT_TRIES_LIMIT) {
self.handleDisconnect();
};
});
};

this.connect = function (callback) {
var self = this
, cb = callback || function () {};
this.client.connect(function (err, data) {
if (err) {
self.emit('error', err);
cb(err);
if (reconnectAttempts < RECONNECT_TRIES_LIMIT) {
self.handleDisconnect();
} else {
self.emit('error', err);
cb(err);
}
}
else {
reconnectAttempts = 0;
self.exec('USE ' + self.config.database, function (err, data) {
if (err) {
self.emit('error', err);
Expand All @@ -64,6 +84,7 @@ utils.mixin(Adapter.prototype, new (function () {
});
}
});

};

this.disconnect = function (callback) {
Expand Down

0 comments on commit b657333

Please sign in to comment.