I'm working on a program by linking node js and mysql.
Since it's a remote server in mysql, it's cutting off easily.
And so... I'm going to log in, handle the query, and... I'll hang up I tried to reconnect when needed to process the query and cut it off.
The first one works, but the second connection causes an error.
conn.connect() conn.query() conn.end()
If we go in this order, we'll connect and throw queries... Don't you end it and stuff like that? Of course, it's asynchronous, so through the callback function... I handled it.
The problem occurs when the second connection is attached. What should I do in this case?
======= This is the sauce I made =============================
var mysql = require('mysql');
var dbconfig = require('../js/database.js');
var connection = mysql.createConnection(dbconfig); //< DB Connection~!!
var sleep = require('system-sleep');
connection.connect(function(err){
if(!err) {
console.log("Database is connected ... nn");
} } else {
console.log("Error connecting database ... nn:" + err);
}
varsql = "SELECT day_reset_time, night_reset_time FROM reset_table WHERE week_day='Thursday'";
connection.query(sql, function(err, rows){
if(err) throw err;
day_reset_time = rows[0].day_reset_time;
night_reset_time = rows[0].night_reset_time;
console.log('day_reset_time:' + day_reset_time + ', night_reset_time:' + night_reset_time);
connection.end();
});
});
sleep(10000); //< stop here for 10 seconds~!!
connection.connect(function(err){
if(!err) {
console.log("Database is connected ... nn");
} } else {
console.log("Error connecting database ... nn:" + err);
}
varsql = "SELECT day_reset_time, night_reset_time FROM reset_table WHERE week_day='Thursday'";
connection.query(sql, function(err, rows){
if(err) throw err; //< Cannot enqueue Query after invoking quit.
day_reset_time = rows[0].day_reset_time;
night_reset_time = rows[0].night_reset_time;
console.log('day_reset_time:' + day_reset_time + ', night_reset_time:' + night_reset_time);
connection.end();
});
});
Processing results
C:\nodeProj\voo\js>node test.js
Database is connected ... nn
day_reset_time:06:55, night_reset_time:16:05
Error connecting database ... nn:Error: Cannot enqueue Handshake after invoking quit.
C:\nodeProj\valeo\js\test.js:43
if(err) throw err;
^
Error: Cannot enqueue Query after invoking quit.
at Protocol._validateEnqueue (C:\nodeProj\valeo\node_modules\mysql\lib\protocol\Protocol.js:204:16)
at Protocol._enqueue (C:\nodeProj\valeo\node_modules\mysql\lib\protocol\Protocol.js:139:13)
at Connection.query (C:\nodeProj\valeo\node_modules\mysql\lib\Connection.js:208:25)
at Handshake._callback (C:\nodeProj\valeo\js\test.js:41:16)
at Handshake.Sequence.end (C:\nodeProj\valeo\node_modules\mysql\lib\protocol\sequences\Sequence.js:88:24)
at C:\nodeProj\valeo\node_modules\mysql\lib\protocol\Protocol.js:225:14
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickCallback (internal/process/next_tick.js:180:9)
at Function.Module.runMain (module.js:667:11)
at startup (bootstrap_node.js:187:16)
Self-answer:
Sleep on the next line...
connection = mysql.createConnection(dbconfig);
If you put it in, it'll be solved.
I'm glad it worked out.
© 2024 OneMinuteCode. All rights reserved.