【问题标题】:Connection closed before request completed连接在请求完成前关闭
【发布时间】:2022-12-06 06:40:29
【问题描述】:

我正在尝试使用 NodeTedious 库连接到 Azure SQL DB,但它失败并出现以下错误。此错误消息使我无处可去。

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.

连接代码:

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);
}

符合以下 Microsoft 推荐的连接字符串: https://learn.microsoft.com/en-us/azure/azure-sql/database/connect-query-nodejs?view=azuresql&tabs=windows

【问题讨论】:

标签: node.js sql-server azure-sql-database tedious


【解决方案1】:

显然,您的请求函数早在完成任何这些查询之前就返回了。

此外,您的 foreach 可能有问题。但是,将 'await' 放入 .forEach() 循环中不会暂停该循环的执行。

尝试在数据库调用中包含正确的 await 并检查它是否正确

【讨论】:

    【解决方案2】:

    澄清这个线程 - 在您要求执行请求之前连接已关闭。 Microsoft 文档提供的示例包含错误 - 我已经打开了一个问题 - 因此要解决问题,只需在收到请求上的“完成”事件时要求关闭连接,在事件“连接”后删除连接关闭连接对象:

    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`);
          }
        }
      );
    
      request.on("row", columns => {
        columns.forEach(column => {
          console.log("%s	%s", column.metadata.colName, column.value);
        });
      });
    
      request.on("done", (rowCount) => {
         connection.close();
      });
    
      connection.execSql(request);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-22
      • 1970-01-01
      • 2020-11-05
      相关资源
      最近更新 更多