节点。当收到多个请求时,js SQL服务器崩溃

时间:2022-04-12 15:45:51

I have a NodeJS application which is my server and I created a Database class to help me handle querying my SQL DB. If I send requests a second between each other, everything runs fine.. no problems.. But if I start spamming requests to my server it crashes due to Error: Cannot enqueue Quit after invoking quit.

我有一个NodeJS应用程序,它是我的服务器,我创建了一个数据库类来帮助我处理查询我的SQL数据库。如果我互相发送一秒钟的请求,一切都会好起来的。没有问题. .但是,如果我开始向我的服务器发送垃圾邮件请求,它会因为错误而崩溃:在调用Quit之后无法排队退出。

Here's my query function inside my Database class

这是我的数据库类中的查询函数

static query(query: string): Promise<any> {
    console.log('Query: ' + query);
    return new Promise((resolve, reject) => {
        this.connect().then(success => {
            sqlConn.query(query, (err, results) => {
                if (err) { return reject(err);
                } else {
                    return resolve(results);
                }
            });
        }).catch(err => {
            return reject(err);
        }).then( () => {
           if (sqlConn.state !== 'disconnected') {
            sqlConn.end();
           }
        });
    });
};

and here's the this.connect() function

这是this。connect()函数

static connect(): Promise<any> {
    return new Promise((resolve, reject) => {
        sqlConn = mysql.createConnection(this.connectionData);
        sqlConn.connect(err => {
            if (err) { return reject(err); } else {
                return resolve('SQL connection established');
            }
        });
    });
};

I'm pretty sure the problem appears sometimes, it would still be processing one query, and then another query comes before the first one finishes, so it would call sqlConn.end() twice, even when it's already disconnected? Any help is greatly appreciated...

我很确定问题有时会出现,它仍然在处理一个查询,然后在第一个查询结束之前出现另一个查询,因此它会调用sqlConn.end()两次,即使它已经断开连接?非常感谢您的帮助……

> Main goal is for the query to wait till it's 100% done before it runs the next one..

>的主要目标是让查询等到100%完成后再运行下一个。

2 个解决方案

#1


1  

You can simplify your code by using the npm module mysql and use it's built-in connection pool.

您可以使用npm模块mysql来简化代码,并使用它的内置连接池。

From the documentation:

从文档:

var mysql = require('mysql');
var pool  = mysql.createPool({
  connectionLimit : 10,
  host            : 'example.org',
  user            : 'bob',
  password        : 'secret',
  database        : 'my_db'
});

pool.query('SELECT 1 + 1 AS solution', function (error, results, fields) {
  if (error) throw error;
  console.log('The solution is: ', results[0].solution);
});

You can, of course, create your own function that promisifies that call like this:

当然,你可以创建你自己的函数来实现这个调用:

function query (sql) {
  return new Promise((resolve, reject) => {
    pool.query(sql, (error, results, fields) => 
      error ? reject(error) : resolve({ results, fields });
  };
}     

#2


1  

If you really wants to use this approach then please use eachSeries function of async library.

如果您真的想使用这种方法,请使用异步库的每个系列函数。

var chunkedArray= [];
async.eachSeries(chunkedArray, startUpload, endUpload);

funtion startUpload(data,cb){
    //iterate over every single item in array 1 at a time
    }

function endUplaod(err){

//finally call this
}

This might help:- https://caolan.github.io/async/docs.html#eachSeries

这可能帮助:https://caolan.github.io/async/docs.html eachSeries

But i rather suggest you to use pooling of connection which make less overhead on your db and you can use your mysql more efficiently then making multiple connection.

但是我建议您使用连接池,这样可以减少对db的开销,并且可以更有效地使用mysql,而不是进行多个连接。

// Load module
var mysql = require('mysql');
// Initialize pool
var pool      =    mysql.createPool({
    connectionLimit : 10,
    host     : '127.0.0.1',
    user     : 'root',
    password : 'root',
    database : 'db_name',
    debug    :  false
});    
module.exports = pool;

#1


1  

You can simplify your code by using the npm module mysql and use it's built-in connection pool.

您可以使用npm模块mysql来简化代码,并使用它的内置连接池。

From the documentation:

从文档:

var mysql = require('mysql');
var pool  = mysql.createPool({
  connectionLimit : 10,
  host            : 'example.org',
  user            : 'bob',
  password        : 'secret',
  database        : 'my_db'
});

pool.query('SELECT 1 + 1 AS solution', function (error, results, fields) {
  if (error) throw error;
  console.log('The solution is: ', results[0].solution);
});

You can, of course, create your own function that promisifies that call like this:

当然,你可以创建你自己的函数来实现这个调用:

function query (sql) {
  return new Promise((resolve, reject) => {
    pool.query(sql, (error, results, fields) => 
      error ? reject(error) : resolve({ results, fields });
  };
}     

#2


1  

If you really wants to use this approach then please use eachSeries function of async library.

如果您真的想使用这种方法,请使用异步库的每个系列函数。

var chunkedArray= [];
async.eachSeries(chunkedArray, startUpload, endUpload);

funtion startUpload(data,cb){
    //iterate over every single item in array 1 at a time
    }

function endUplaod(err){

//finally call this
}

This might help:- https://caolan.github.io/async/docs.html#eachSeries

这可能帮助:https://caolan.github.io/async/docs.html eachSeries

But i rather suggest you to use pooling of connection which make less overhead on your db and you can use your mysql more efficiently then making multiple connection.

但是我建议您使用连接池,这样可以减少对db的开销,并且可以更有效地使用mysql,而不是进行多个连接。

// Load module
var mysql = require('mysql');
// Initialize pool
var pool      =    mysql.createPool({
    connectionLimit : 10,
    host     : '127.0.0.1',
    user     : 'root',
    password : 'root',
    database : 'db_name',
    debug    :  false
});    
module.exports = pool;