Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
错误异步Node.js调用全局sql连接存在/Can';t集标题_Node.js_Sql Server_<img Src="//i.stack.imgur.com/RUiNP.png" Height="16" Width="18" Alt="" Class="sponsor Tag Img">elasticsearch_Async Await - Fatal编程技术网 elasticsearch,async-await,Node.js,Sql Server,elasticsearch,Async Await" /> elasticsearch,async-await,Node.js,Sql Server,elasticsearch,Async Await" />

错误异步Node.js调用全局sql连接存在/Can';t集标题

错误异步Node.js调用全局sql连接存在/Can';t集标题,node.js,sql-server,elasticsearch,async-await,Node.js,Sql Server,elasticsearch,Async Await,我有一个问题,不知怎的,当我调用路由时,我的mssql节点js api连接没有关闭…: Global connection already exists. Call sql.close() first. 或 这是我的MSSQL连接调用: var executeQuery = async (query) => { try { var connection = await sql.connect(dbconfig); // q

我有一个问题,不知怎的,当我调用路由时,我的mssql节点js api连接没有关闭…:

Global connection already exists. Call sql.close() first.

这是我的MSSQL连接调用:

var  executeQuery = async (query) => {   
    try {         
        var connection = await sql.connect(dbconfig);
        // query to the database
        const result = await sql.query(query);

        //console.log(result);

            //console.log("Result:" , result);
            sql.close();
            return result;  


        } catch(err) {
            console.log(err);
            sql.close();
        } finally {
            sql.close();
        }                 
}
这里是用于Elasticsearch的:

const searchES =  async (req,res) => {
    const result = await elasticclient.search({
                        index: 'goldstandard-search-write',
                        type: 'joboffer',
                        q: 'namespace:pool'
                     });
}
我想等待这两个结果回来与axios进行比较:

router.get("/api/compareCount", async function(req,res) {
    var query = `SELECT COUNT(*) as count
      FROM [DublettenReferenzmenge].[dbo].[DocumentForElasticsearch] where lastChange < dateadd(day,-1,getdate())`;
      var query2 = `SELECT COUNT(*) as count
      FROM [DublettenReferenzmenge].[dbo].[DocumentForElasticsearch] where lastChange < dateadd(hour,-8,getdate())`;
    axios.all([searchES(req,res), await executeQuery(query), await executeQuery(query2)])
    .then(axios.spread(function (esCount, mssqlCount1, mssqlCount2) {
        totalES = esCount.hits.total;
        totalMSSQL = mssqlCount1.recordset[0].count;
        totalMSSQL2 = mssqlCount2.recordset[0].count;
        console.log(" esCount: ", totalES, "\n", " mssqlCount: ", totalMSSQL,  " mssqlCount2: ", totalMSSQL2);
        //var result = esCount == mssqlCount;
        var msg = "ES Dokumente total: " + totalES + " MSSQL Dokumente total: " + totalMSSQL + "<br>";
        msg += "Critical: ";
        msg += totalES != totalMSSQL +"<br>";
        msg += "Warning: " + (totalES != totalMSSQL2);
        res.set('Content-Type', 'text/html');
        res.send(msg);
    })).catch((err) => {
        res.send(err);
    });
})
我用两个不同的sql查询调用了两次executeQuery函数。但是,我得到了一个错误,我的sql连接没有关闭,或者我的头没有设置,我不知道为什么..:

编辑:我想这可能是某种比赛条件。。。如果我一次又一次地快速重新加载路线,就会出现此错误。。似乎已经有一个与该主机的持久化sql连接打开了,实际上它关闭该连接的速度不够快,无法在我再次调用相同的路由时关闭。。但除此之外,如果我正常地重新加载页面,它就会工作

我认为我必须在server.js级别建立sql连接,并以某种方式返回连接以使其可用…或者以面向对象的方式执行此操作,使我成为一个连接对象来操作和发送查询,并且只有在关闭浏览器后才将其销毁等等。。。但我不知道怎么做

router.get("/api/compareCount", async function(req,res) {
    var query = `SELECT COUNT(*) as count
      FROM [DublettenReferenzmenge].[dbo].[DocumentForElasticsearch] where lastChange < dateadd(day,-1,getdate())`;
      var query2 = `SELECT COUNT(*) as count
      FROM [DublettenReferenzmenge].[dbo].[DocumentForElasticsearch] where lastChange < dateadd(hour,-8,getdate())`;
    axios.all([searchES(req,res), await executeQuery(query), await executeQuery(query2)])
    .then(axios.spread(function (esCount, mssqlCount1, mssqlCount2) {
        totalES = esCount.hits.total;
        totalMSSQL = mssqlCount1.recordset[0].count;
        totalMSSQL2 = mssqlCount2.recordset[0].count;
        console.log(" esCount: ", totalES, "\n", " mssqlCount: ", totalMSSQL,  " mssqlCount2: ", totalMSSQL2);
        //var result = esCount == mssqlCount;
        var msg = "ES Dokumente total: " + totalES + " MSSQL Dokumente total: " + totalMSSQL + "<br>";
        msg += "Critical: ";
        msg += totalES != totalMSSQL +"<br>";
        msg += "Warning: " + (totalES != totalMSSQL2);
        res.set('Content-Type', 'text/html');
        res.send(msg);
    })).catch((err) => {
        res.send(err);
    });
})
axios.all([searchES(req,res), await executeQuery(query), await executeQuery(query2)])