node-mssql: Connection Error: Connection is Closed
Hi…
I’m able to access my MSSQL from MSQL Studio. defined my index.js file as below, but it keeps give me this error:
{ [ConnectionError: Connection is closed.] name: ‘ConnectionError’, message: ‘Connection is closed.’, code: ‘ECONNCLOSED’}
what wrong I’m doing, if below is my code, thanks
var sql = require('mssql');
var config = { user: 'sa', password: 'bk123', server: 'HASN-BK\\SQLEXPRESS', database: 'BK',
stream: true }
sql.connect(config, function(err) {
var request = new sql.Request();
request.stream = true;
request.query('SELECT * from emp');
request.on('recordset', function(columns) {
console.log(columns);
// Emitted once for each recordset in a query
});
request.on('row', function(row) {
console.log(row);
// Emitted for each row in a recordset
});
request.on('error', function(err) {
console.log(err);
// May be emitted multiple times
});
request.on('done', function(returnValue) {
// Always emitted as the last one
});
});
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Comments: 40
I have the same issue when I run 2 or more queries one right after the other
Worked fine for me, better use a global connection rather connecting to the databse each time you have to make a request to avoid the ‘Connection is closed Error’.
I’m finding rampant “Connection is Closed” errors when attempting to paralellize multiple requests using the native
Promise.all.For example if I try to run 4 queries in parallel to SELECT from the same table, the first Request in the
Promise.allreports the error.If I try to run 4
INSERTs requests in parallel into the same table (yes I’m familiar withbulk), the firstRequestin thePromise.allarray reports the error, but the last Request succeeds with insertion (as verified by direct SQL calls to the server).I believe this bug is related to the Tedious Bug.
I found a solution 😃 Ignore the samples on the page and only use 1 connection:
var config = { user: ‘xxxxx’, password: 'xxxxxxxxx, server: ‘servfername.database.windows.net’, // You can use ‘localhost\instance’ to connect to named instance database: 'db-name, options: { encrypt: true // Use this if you’re on Windows Azure } }
var connection = new sql.Connection(config); connection.connect();
var request = new sql.Request(connection); request.input(‘myEmail’, sql.VarChar, req.params.user); request.input(‘myPass’, sql.VarChar, req.params.pass); var sqlquery = “SELECT * From USERTable WHERE Email=@myEmail AND Password=@myPass”; request.query(sqlquery, function (err, recordset) { if (err) res.json(err); else res.json(recordset); });
@dw1284’s solution fixed the “Connection is closed” error for me.
Changed from:
Changed to:
I recently started getting
connection closedissues as well. I’ve never closed any connection in my application code. Should I? My requests typically look like this:Should I rather do like this?
Just to add my two cents. I’m getting an error on Azure, though it works locally.
I am using
encrypt:true. It’s kinda perplexing. Let me know if you have any ideas.I always got the error “sql.Connection is not a constructor”, why?
Having the same issue, everything is working locally and when published to Azure I get the same error… Any solution?