M
M
Maxim Nine2017-03-22 13:47:26
MySQL
Maxim Nine, 2017-03-22 13:47:26

How to stop sql query?

There is a code:

var sql = require("mysql");
var connection = sql.createConnection({
    host: 'localhost',
    user: 'root',
    password: 'password',
    database: 'chat'
});

app.get('/', function(req, res) {
    connection.connect();
    var loga = "kulonful";
    connection.query('select * from users where login = "'+loga+'"', function(err, result){
        if(err) { res.send('Произошла критическая ошибка'); }
        // здесь
        res.send(result);
    });
})

In the place "here" you need to stop the connection sql-query, otherwise the next time the script will give an error.
What is the function of stop?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
hoarywolf, 2017-03-22
@hoarywolf

Because connection.connect shouldn't be inside app.get, what do you think should happen if multiple users access your server at the same time? connection you have a global variable, each of the users will try to connect to it, although it is already connected and is processing another user's request at this time.
You either need to take out connect and make a connection as soon as the script starts, or use a connection pool and allocate connections from it to each user.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question