Skip to content

Commit

Permalink
mysql set to try to keep server connection alive
Browse files Browse the repository at this point in the history
  • Loading branch information
Dave Conway-Jones committed Feb 26, 2017
1 parent 5a85813 commit c6d214f
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 37 deletions.
85 changes: 50 additions & 35 deletions storage/mysql/68-mysql.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,62 +17,77 @@ module.exports = function(RED) {
this.setMaxListeners(0);
var node = this;

function checkVer() {
node.connection.query("SELECT version();", [], function(err, rows) {
if (err) {
node.connection.release();
node.error(err,msg);
node.status({fill:"red",shape:"ring",text:"Bad Ping"});
doConnect();
}
});
}

function doConnect() {
node.connecting = true;
node.emit("state","connecting");
node.connection = mysqldb.createConnection({
host : node.host,
port : node.port,
user : node.credentials.user,
password : node.credentials.password,
database : node.dbname,
timezone : node.tz,
insecureAuth: true,
multipleStatements: true
});
if (!node.pool) {
node.pool = mysqldb.createPool({
host : node.host,
port : node.port,
user : node.credentials.user,
password : node.credentials.password,
database : node.dbname,
timezone : node.tz,
insecureAuth: true,
multipleStatements: true,
connectionLimit: 25
});
}

node.connection.connect(function(err) {
node.pool.getConnection(function(err, connection) {
node.connecting = false;
if (err) {
node.error(err);
node.emit("state",err.code);
node.error(err);
node.tick = setTimeout(doConnect, reconnect);
} else {
}
else {
node.connection = connection;
node.connected = true;
node.emit("state","connected");
}
});

node.connection.on('error', function(err) {
node.connected = false;
node.emit("state",err.code);
if (err.code === 'PROTOCOL_CONNECTION_LOST') {
doConnect(); // silently reconnect...
} else {
node.error(err);
doConnect();
node.connection.on('error', function(err) {
node.connected = false;
node.connection.release();
node.emit("state",err.code);
if (err.code === 'PROTOCOL_CONNECTION_LOST') {
doConnect(); // silently reconnect...
}
else if (err.code === 'ECONNRESET') {
doConnect(); // silently reconnect...
}
else {
node.error(err);
doConnect();
}
});
if (!node.check) { node.check = setInterval(checkVer, 290000); }
}
});
}

this.connect = function() {
if (!this.connected && !this.connecting) { doConnect(); }
if (this.connected) { node.emit("state","connected"); }
else { node.emit("state","connecting"); }
if (!this.connected && !this.connecting) {
doConnect();
}
}

this.on('close', function (done) {
if (this.tick) { clearTimeout(this.tick); }
if (this.check) { clearInterval(this.check); }
node.connected = false;
node.emit("state"," ");
if (this.connection) {
node.connection.end(function(err) {
if (err) { node.error(err); }
done();
});
} else {
done();
}
node.pool.end(function (err) { done(); });
});
}
RED.nodes.registerType("MySQLdatabase",MySQLNode, {
Expand Down
4 changes: 2 additions & 2 deletions storage/mysql/package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name" : "node-red-node-mysql",
"version" : "0.0.13",
"version" : "0.0.14",
"description" : "A Node-RED node to read and write to a MySQL database",
"dependencies" : {
"mysql" : "^2.12.0"
"mysql" : "^2.13.0"
},
"repository" : {
"type":"git",
Expand Down

0 comments on commit c6d214f

Please sign in to comment.