I used nodejs to connect to a SQL Server. But I failed to create a global connection that is shared among all queries. In the documentation, queries are all wrapped in connection call back function, which means every time I make a query, I have to establish a connection.
Is there a way to keep a single connection alive so I can share it with all controllers? I've done it with mongodb, not sure how SQL Server does this.
I did something like this
connection.js:
const config = require('./config')
const sql = require('mssql')
const pool = sql.ConnectionPool(config).connect(function(err){
if(err) throw err;
console.log("connected");
});
module.exports = {
sql, pool
}
server.js:
const conn = require('./connection.js')
const requst = new conn.sql.Request(conn.pool)
request.query('select * from table', function (err, recordset){
if(err) console.log(err);
console.log(recordset);
});
It failed because the connection is closed.
Please share some of your insights, thanks