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,62 +17,77 @@ module.exports = function(RED) {
this.setMaxListeners(0); this.setMaxListeners(0);
var node = this; var node = this;
function doConnect() { function checkVer() {
node.connecting = true; node.connection.query("SELECT version();", [], function(err, rows) {
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
});
node.connection.connect(function(err) {
node.connecting = false;
if (err) { if (err) {
node.error(err); node.connection.release();
node.emit("state",err.code); node.error(err,msg);
node.tick = setTimeout(doConnect, reconnect); node.status({fill:"red",shape:"ring",text:"Bad Ping"});
} else {
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(); doConnect();
} }
}); });
} }
function doConnect() {
node.connecting = true;
node.emit("state","connecting");
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.pool.getConnection(function(err, connection) {
node.connecting = false;
if (err) {
node.emit("state",err.code);
node.error(err);
node.tick = setTimeout(doConnect, reconnect);
}
else {
node.connection = connection;
node.connected = true;
node.emit("state","connected");
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() { 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",