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

前端 未结 6 1624
暗喜
暗喜 2021-01-03 23:09

I\'m trying to create web services using node.js from an sql server database,in the frontend when i call those 2 webservices simultaneously it throws an error Global connect

6条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-03 23:50

    Don't read their documentation, I don't think it was written by someone that actually uses the library :) Also don't pay any attention to the names of things, a 'ConnectionPool' doesn't seem to actually be a connection pool of any sort. If you try and create more than one connection from a pool, you will get an error. This is the code that I eventually got working:

    const sql = require('mssql');
    
    let pool = new sql.ConnectionPool(config); // some object that lets you connect ONCE
    let cnn = await pool.connect(); // create single allowed connection on this 'pool'
    let result = await cnn.request().query(query);
    console.log('result:', result);
    cnn.close(); // close your connection
    return result;
    

    This code can be run multiple times in parallel and seems to create multiple connections and correctly close them.

提交回复
热议问题