1

I've got this script for reading a file and then insert the data into mysql tables. The script works, but it hangs, so I have to press CTRL-C to stop the script. But the script should stop normally, what do I have to change?

var fs = require('fs');
var filename;
var myGID;
filename = "data/insertUser1_next.json";

function get_line(filename, line_no, callback) {
fs.readFile(filename, function (err, data) {
if (err) throw err;

  // Data is a buffer that we need to convert to a string
  // Improvement: loop over the buffer and stop when the line is reached
  var lines = data.toString('utf-8').split("\n");

  if(+line_no > lines.length){
    return callback('File end reached without finding line', null);
  }

  // lines
  callback(null, lines[0], lines[1], lines[2], lines[3]);
});
}

get_line(filename, 0, function(err, line, line2, line3, line4){


line = line.replace(/(\r\n|\n|\r)/gm,"");
line2 = line2.replace(/(\r\n|\n|\r)/gm,"");
line3 = line3.replace(/(\r\n|\n|\r)/gm,"");
/*line4 = line4.replace(/(\r\n|\n|\r)/gm,"");*/
console.log('The line: ' + line);
console.log('The line2: ' + line2);
console.log('The line3: ' + line3);
console.log('The line4: ' + line4);

var post  = {gid: line, uid: line2};
var post2  = {uid: line2, displayname: line3, password: line4};

var mysql      = require('mysql');    
var db_config = {
    host     : '123.456.789.012',
    user     : 'user',
    password : 'password',
    database : 'maindata'
};

var con = mysql.createPool(db_config);

    con.getConnection(function(err){
                if (err) {
                    console.log(err);
                    return;
                }
        con.query('INSERT INTO group_user SET ?', post, function(err, result) {
                if (err) {
                    console.log(err);
                    return;
                }

        });
        con.query('INSERT INTO users SET ?', post2, function(err, result) {
                if (err) {
                    console.log(err);
                    return;
                }

        });
    });
});

3 Answers 3

1

Here you can see what happened:

When you are done using the pool, you have to end all the connections or the Node.js event loop will stay active until the connections are closed by the MySQL server. This is typically done if the pool is used in a script or when trying to gracefully shutdown a server. To end all the connections in the pool, use the end method on the pool:

pool.end(function (err) {
  // all connections in the pool have ended
});

So, if you place con.end() after your queries are done, the script will terminate normally

Sign up to request clarification or add additional context in comments.

3 Comments

hi, thank you. i tried to put con.end(); after the second con.query part but i get [Error: Pool is closed] twice and insert statement was not excecuted.
It's asynchronous so you need to close it in the callback of the last query to run.
con.end() is deprecated, so, con.release() should be used in stead.
0

The following statement will close the connection ensuring that all the queries in the queue are processed. Please note that this is having a callback function.

connection.end(function(err){
// Do something after the connection is gracefully terminated.

});

The following statement will terminate the assigned socket and close the connection immediately. Also there is no more callbacks or events triggered for the connection.

 connection.destroy();

Comments

0

hey I suggest to install forever and start node servers.js with forever you dont need any terminal open.

And you need to close you mysql connection at the end to stop you hangs problem, i think.

npm install -g forever
npm install forever

//FOR your Problem

con.end(function(err){
// Do something after the connection is gracefully terminated.
});

con.destroy();

The following statement will close the connection ensuring that all the queries in the queue are processed. Please note that this is having a callback function.

connection.end(function(err){
// Do something after the connection is gracefully terminated.
});

The following statement will terminate the assigned socket and close the connection immediately. Also there is no more callbacks or events triggered for the connection.

 connection.destroy();

1 Comment

hey, thanks for your reply. i tried it but con.end(); at the end of the script does not work. i also tried to put con.end() at the end of the getconnection part and get [Error: Pool is closed]. can you give me a hint how to get the script work?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.