1
0
mirror of https://github.com/node-red/node-red-nodes.git synced 2023-10-10 13:36:58 +02:00

mysql set to try to keep server connection alive

This commit is contained in:
Dave Conway-Jones 2017-02-26 10:18:07 +00:00
parent 5a85813a45
commit c6d214fcfe
2 changed files with 60 additions and 45 deletions

View File

@ -17,10 +17,22 @@ module.exports = function(RED) {
this.setMaxListeners(0); this.setMaxListeners(0);
var node = this; 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() { function doConnect() {
node.connecting = true; node.connecting = true;
node.emit("state","connecting"); node.emit("state","connecting");
node.connection = mysqldb.createConnection({ if (!node.pool) {
node.pool = mysqldb.createPool({
host : node.host, host : node.host,
port : node.port, port : node.port,
user : node.credentials.user, user : node.credentials.user,
@ -28,51 +40,54 @@ module.exports = function(RED) {
database : node.dbname, database : node.dbname,
timezone : node.tz, timezone : node.tz,
insecureAuth: true, insecureAuth: true,
multipleStatements: true multipleStatements: true,
connectionLimit: 25
}); });
}
node.connection.connect(function(err) { node.pool.getConnection(function(err, connection) {
node.connecting = false; node.connecting = false;
if (err) { if (err) {
node.error(err);
node.emit("state",err.code); node.emit("state",err.code);
node.error(err);
node.tick = setTimeout(doConnect, reconnect); node.tick = setTimeout(doConnect, reconnect);
} else { }
else {
node.connection = connection;
node.connected = true; node.connected = true;
node.emit("state","connected"); node.emit("state","connected");
}
});
node.connection.on('error', function(err) { node.connection.on('error', function(err) {
node.connected = false; node.connected = false;
node.connection.release();
node.emit("state",err.code); node.emit("state",err.code);
if (err.code === 'PROTOCOL_CONNECTION_LOST') { if (err.code === 'PROTOCOL_CONNECTION_LOST') {
doConnect(); // silently reconnect... doConnect(); // silently reconnect...
} else { }
else if (err.code === 'ECONNRESET') {
doConnect(); // silently reconnect...
}
else {
node.error(err); node.error(err);
doConnect(); doConnect();
} }
}); });
if (!node.check) { node.check = setInterval(checkVer, 290000); }
}
});
} }
this.connect = function() { this.connect = function() {
if (!this.connected && !this.connecting) { doConnect(); } if (!this.connected && !this.connecting) {
if (this.connected) { node.emit("state","connected"); } doConnect();
else { node.emit("state","connecting"); } }
} }
this.on('close', function (done) { this.on('close', function (done) {
if (this.tick) { clearTimeout(this.tick); } if (this.tick) { clearTimeout(this.tick); }
if (this.check) { clearInterval(this.check); }
node.connected = false; node.connected = false;
node.emit("state"," "); node.emit("state"," ");
if (this.connection) { node.pool.end(function (err) { done(); });
node.connection.end(function(err) {
if (err) { node.error(err); }
done();
});
} else {
done();
}
}); });
} }
RED.nodes.registerType("MySQLdatabase",MySQLNode, { RED.nodes.registerType("MySQLdatabase",MySQLNode, {

View File

@ -1,9 +1,9 @@
{ {
"name" : "node-red-node-mysql", "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", "description" : "A Node-RED node to read and write to a MySQL database",
"dependencies" : { "dependencies" : {
"mysql" : "^2.12.0" "mysql" : "^2.13.0"
}, },
"repository" : { "repository" : {
"type":"git", "type":"git",