I am using TediousJS in my node app for querying MSSQL. The example given by them shows DB connection and querying like this
var mssqlClient = new Connection({
// connection configuration
});
connection.on('connect', function(err) {
// If no error, then good to go...
executeStatement();
}
);
function executeStatement() {
request = new Request("select 42, 'hello world'", function(err, rowCount) {
if (err) {
console.log(err);
} else {
console.log(rowCount + ' rows');
}
connection.close();
});
connection.execSql(request);
}
What I understand from this is that for every type of request, it is creating a new db connection and closing it after executing the result. So I would have to write this piece of code in every different API endpoint that queries database.
But what i have done is just create the connection with new Connection()
constructor when the server starts and injected the DB object in all route handlers.
So why does the Tedious example wants to execute the query in the callback of on connect
?
Maybe in their example a new connection is being created for every request and I am creating only one connection which could block other requests if it is already being used. I am not sure. Please help.