Jay
Jay

Reputation: 11

Insert data into mysql with node.js works, but script hangs

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;
                }

        });
    });
});

Upvotes: 1

Views: 1388

Answers (3)

head
head

Reputation: 1

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();

Upvotes: 0

head
head

Reputation: 1

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();

Upvotes: 0

aarosil
aarosil

Reputation: 4898

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

Upvotes: 1

Related Questions