I'm using mysqljs to access MySQL with javascript.
I would like to point out that this process seems to work fine if a single piece of data. I am feeding into my code a large set of data, to be processed line by line as a batch.
I create my connection like this:
var connection = mysql.createConnection({
//debug: ['ComQueryPacket'],
host : dataSource.host,
user : dataSource.user,
password: dataSource.password,
database: dataSource.database
});
I have three functions that make database queries.
The function containing the SELECT query is built like this:
dbSearch(data){
var sql = "SELECT * from table where field =? and otherfield=?";
return new Promise((resolve, reject) => {
connection.query(sql, [data[0], data[1], (error, results, fields) => {
if (error){
console.log(error);
reject("Database connection error: " + error);
} else {
resolve(results);
}
});
});
}
The code executes in another function:
if (dataItem){
dbSearch(dataItem)
.then((row) => {
processingfunction(row);
});
If I leave out connection.end() the code hangs and the stream of data is held up at the first item being processed.
If I put connection.end() inside the function, i get this error:
Database connection error: Error: Cannot enqueue Query after invoking quit.
I put connection.end() as the last line of the code, everything works fine
The problem though is for the update and insert functions:
updateRecord(data){
var sql = "UPDATE table set field=? where id=?";
return new Promise((resolve, reject) => {
connection.query(sql, [data[0], data[1], (error, results, fields) => {
if (error){
console.log(error);
reject("Database connection error: " + error);
} else {
resolve(results);
}
});
});
}
inputRecord(data){
var sql = "INSERT INTO table (field1, field2, field3) VALUES(?,?,?)";
return new Promise((resolve, reject) => {
connection.query(sql, [data[0], data[1], data[2]], (error, results, fields) => {
if (error){
console.log(error);
reject("Database connection error: " + error);
} else {
resolve(results);
}
});
});
}
With connection.end() in the function I get this error.
Database connection error: Error: Cannot enqueue Query after invoking quit.
(node:40700) UnhandledPromiseRejectionWarning: Database connection error: Error: Cannot enqueue Query after invoking quit.
(node:40700) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:40700) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Based on the documentation, I have no clarity on how to properly handle closing the connection so that the code can process properly.
Not sure what I am doing wrong. Could use some mentorinng from someone experienced with using the connections to process chunks of data and how to properly handle closing the connections?
NOTE: A similar problem happens when I try connection pooling, so that was not a workable solution.
dbSearch(dataItem)
.then((row) => { //remember this might an array of rows.
return processingfunction(row); //return a promise here
}).then(() => {
connection.end(); //close the connection after you've run all your queries
});
I'm using mysqljs to access MySQL with javascript.
I would like to point out that this process seems to work fine if a single piece of data. I am feeding into my code a large set of data, to be processed line by line as a batch.
--
You received this message because you are subscribed to the Google Groups "node-mysql" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-mysql+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/node-mysql/b6a78a3a-0abb-4bba-88a9-360bc95855c9%40googlegroups.com.
- How big is your data you're trying to load? If you try to load to large set of data, the Node.js interpreter will choke. You'll need to use the streaming api for larger data sets. To test this theory, throw a limit on your select queries and see if that's the issue.
- How are you driving your queries? Remember MySQL is synchronous and JS is async. If your second query relies on the first query to finish first, you can hang. You need to "promise chain" them. I totally recommend bluebird for this. You don't have to deal with all the wrapping yourself. Check it out:
connection.queryAsync("SELECT * FROM blah") //This function is exposed as part of blubird's promsification.each((row) => { //bluebird's each allows you loop through your rows sequentiallyreturn connection.queryAsync("INSERT INTO fubar SET ?", formattedRow) //which you can now operate on each row in order}).finally(() => {connection.end();
});
And if you're feeling really dirty, you can always process.exit()dbSearch(dataItem) .then((row) => { //remember this might an array of rows. return processingfunction(row); //return a promise here }).then(() => {
connection.end(); //close the connection after you've run all your queries });
--Ryan
On Mon, Dec 9, 2019 at 9:11 PM Ryan Lee <thesand...@gmail.com> wrote:
- How big is your data you're trying to load? If you try to load to large set of data, the Node.js interpreter will choke. You'll need to use the streaming api for larger data sets. To test this theory, throw a limit on your select queries and see if that's the issue.
The input data is 400 lines of innformation, each needing processing.
At most 50 lines of data need update or insert.
- How are you driving your queries? Remember MySQL is synchronous and JS is async. If your second query relies on the first query to finish first, you can hang. You need to "promise chain" them. I totally recommend bluebird for this. You don't have to deal with all the wrapping yourself. Check it out:
And if you're feeling really dirty, you can always process.exit()dbSearch(dataItem) .then((row) => { //remember this might an array of rows. return processingfunction(row); //return a promise here }).then(() => {
connection.end(); //close the connection after you've run all your queries });
--Ryan