0

I am trying to connect to Azure SQL DB using Tedious library of Node but it fails with the below error. This error message is leading me nowhere.

2022-10-11T10:29:27.876235712Z: [INFO]  RequestError: Connection closed before request completed.
2022-10-11T10:29:27.876281315Z: [INFO]      at Connection.cleanupConnection (/app/node_modules/tedious/lib/connection.js:1115:21)
2022-10-11T10:29:27.876288116Z: [INFO]      at Connection.enter (/app/node_modules/tedious/lib/connection.js:2893:12)
2022-10-11T10:29:27.876292416Z: [INFO]      at Connection.transitionTo (/app/node_modules/tedious/lib/connection.js:1358:24)
2022-10-11T10:29:27.876296616Z: [INFO]      at Connection.close (/app/node_modules/tedious/lib/connection.js:1058:10)
2022-10-11T10:29:27.876300717Z: [INFO]      at Connection.<anonymous> (/app/database/sql.database.js:30:14)
2022-10-11T10:29:27.876305517Z: [INFO]      at Connection.emit (node:events:513:28)
2022-10-11T10:29:27.876310817Z: [INFO]      at Connection.emit (/app/node_modules/tedious/lib/connection.js:1048:18)
2022-10-11T10:29:27.876315218Z: [INFO]      at Connection.processedInitialSql (/app/node_modules/tedious/lib/connection.js:1669:10)
2022-10-11T10:29:27.876321618Z: [INFO]      at /app/node_modules/tedious/lib/connection.js:2723:14
2022-10-11T10:29:27.876325618Z: [INFO]      at processTicksAndRejections (node:internal/process/task_queues:96:5) {
2022-10-11T10:29:27.876329719Z: [INFO]    code: 'ECLOSE',
2022-10-11T10:29:27.876339419Z: [INFO]    number: undefined,
2022-10-11T10:29:27.876344020Z: [INFO]    state: undefined,
2022-10-11T10:29:27.876348120Z: [INFO]    class: undefined,
2022-10-11T10:29:27.876352020Z: [INFO]    serverName: undefined,
2022-10-11T10:29:27.876355821Z: [INFO]    procName: undefined,
2022-10-11T10:29:27.876359821Z: [INFO]    lineNumber: undefined
2022-10-11T10:29:27.876363821Z: [INFO]  }
2022-10-11T10:29:27.885713502Z: [ERROR]  Connection closed before request completed.

Connection Code:

const { Connection, Request } = require('tedious');

// Create connection to database

const config = {
  authentication: {
    options: {
      userName: 'user1', 
      password: 'pass1' 
    },
    type: 'default'
  },
  server: 'abc.sql.net', 
  options: {
    database: 'DB_sb13', 
    encrypt: true
  }
};

const connection = new Connection(config);

// Attempt to connect and execute queries if connection goes through
connection.on("connect", err => {
  if (err) {
    console.error(err.message);
  } else {
    queryDatabase();
  }
  connection.close();
});

connection.connect();

function queryDatabase() {
  console.log("Reading rows from the Table...");

  // Read all rows from table
  const request = new Request(
    `SELECT * from dbo.dbx_test`,
    (err, rowCount) => {
      if (err) {
        console.error(err.message);
      } else {
        console.log(`${rowCount} row(s) returned`);
      }
    }
  );

  request.on("row", columns => {
    columns.forEach(column => {
      console.log("%s\t%s", column.metadata.colName, column.value);
    });
  });

  connection.execSql(request);
}

Connection string as per below Microsoft recommendation: https://learn.microsoft.com/en-us/azure/azure-sql/database/connect-query-nodejs?view=azuresql&tabs=windows

garg10may
  • 5,794
  • 11
  • 50
  • 91
  • What versions of NodeJS and Tedious are you using? – AlwaysLearning Oct 11 '22 at 10:49
  • Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – derpirscher Oct 11 '22 at 10:53

2 Answers2

0

Apparently your request function is returning long before any of those queries are done.

In addition you may have problems in your foreach. However, put a 'await' inside a .forEach() loop doesn't pause execution of that loop.

try to include the proper await in the database calls and check if it corrects

r31sr4r
  • 306
  • 1
  • 2
  • 12
0

To clarify this thread - the connection is closed before you ask to execute the Request. The example provided by Microsoft documentation contains the error - I have opened an issue - So to solve the problem just remove the connection close after the event 'connect' on the connection object, ask to close the connection at the end of the Request :

const { Connection, Request } = require('tedious');

// Create connection to database

const config = {
  authentication: {
    options: {
      userName: 'user1', 
      password: 'pass1' 
    },
    type: 'default'
  },
  server: 'abc.sql.net', 
  options: {
    database: 'DB_sb13', 
    encrypt: true
  }
};

const connection = new Connection(config);

// Attempt to connect and execute queries if connection goes through
connection.on("connect", err => {
  if (err) {
    console.error(err.message);
  } else {
    queryDatabase();
  }
});

connection.connect();

function queryDatabase() {
  console.log("Reading rows from the Table...");

  // Read all rows from table
  const request = new Request(
    `SELECT * from dbo.dbx_test`,
    (err, rowCount) => {
      if (err) {
        console.error(err.message);
      } else {
        console.log(`${rowCount} row(s) returned`);
      }
      // Close the connection
      connection.close();
    }
  );

  request.on("row", columns => {
    columns.forEach(column => {
      console.log("%s\t%s", column.metadata.colName, column.value);
    });
  });

  connection.execSql(request);
}
JBV06
  • 304
  • 2
  • 5