来自云功能的扳手插入的会话泄漏

Session leak on spanner insert from cloud function

我正在尝试使用 post 请求通过云函数将数据插入 Spanner。我想我正在按照文档中的描述做所有事情,我只是不明白是什么导致了下一个错误:

"Error: 1 session leak(s) detected. at _requests.onIdle.then (/srv/node_modules/@google-cloud/spanner/build/src/session-pool.js:193:25) at <anonymous>"

还有我的云函数

const {Spanner} = require('@google-cloud/spanner');

module.exports.http = (req, res) => {
  const projectId = 'project-id';
  const instanceId = 'instance-id';
  const databaseId = 'database-id';

  const spanner = new Spanner({
    projectId: projectId,
  });

  const instance = spanner.instance(instanceId);
  const database = instance.database(databaseId);

  let sqlResponse = "";

  database.runTransaction(async (err, transaction) => {
    if (err) {
      res.status(500).send(JSON.stringify({message: err, requestBody: req.body}));
      return;
    }
    try {
      const data = req.body;
      const [rowCount] = await transaction.runUpdate({
        sql:
          'INSERT Feedbacks (age, comment, gender, rating) VALUES (@age, @comment, @gender, @rating)', 
        params: {
          age: data.age.toString(),
          comment: data.comment,
          gender: data.gender,
          rating: data.rating.toString(),
        },
      });
      sqlResponse = 'Successfully inserted ' + rowCount + ' record into the Feedbacks table.';
      await transaction.commit();
      res.status(200).send(JSON.stringify({message: sqlResponse, requestBody: req.body}));
    } catch (err) {
      res.status(500).send(JSON.stringify({message: err, requestBody: req.body}));
    } finally {
      database.close();
    }
  });

};

您的代码似乎是正确的。正如@Mayeru 在对您的问题的评论中指出的那样,首先要确认的是您正在插入一条新记录,该记录的唯一值是为您的 table 的 primary key 列指定的.

可能导致您遇到的问题的另一种可能性是您正在尝试使用 Cloud Console 的 Cloud Functions > "Function details" 部分的 "Testing" UI 来测试函数.如果是这样,那么当您单击 "Test the function" 按钮时,您可能使用了空请求正文或格式错误的请求正文。在显示在 "Test the function" 按钮上方的 "Triggering event" 文本区域中,确保您输入了有效的 JSON 请求正文,其中包括您的 INSERT 语句期望的元素和值。

例如,像下面这样的 "Triggering event" JSON 请求正文应该可以工作:

{"singerId":"1001","firstName":"Test","lastName":"Singer"}

使用与您共享的代码类似的以下"nodeInsert"函数:

const {Spanner} = require('@google-cloud/spanner');

module.exports.nodeInsert = (req, res) => {

  const projectId = 'my-project';
  const instanceId = 'my-instance';
  const databaseId = 'my-database';

  const spanner = new Spanner({
    projectId: projectId,
  });

  const instance = spanner.instance(instanceId);
  const database = instance.database(databaseId);

  let sqlResponse = "";

  database.runTransaction(async (err, transaction) => {
    if (err) {
      res
        .status(500)
        .send(JSON.stringify({message: err, requestBody: req.body}));
      transaction.end();
      console.error('Transaction terminated.');
      return;
    }
    try {
      const data = req.body;
      const parsedSingerId = parseInt(data.singerId, 10);
      const [rowCount] = await transaction.runUpdate({
        sql:
          'INSERT Singers (SingerId, FirstName, LastName) VALUES (@singerId, @firstName, @lastName)', 
        params: {
          singerId: parsedSingerId,
          firstName: data.firstName,
          lastName: data.lastName,
        },
      });
      sqlResponse = 'Successfully inserted ' + rowCount + ' record into the Singers table.';
      await transaction.commit();
      res
        .status(200)
        .send(JSON.stringify({message: sqlResponse, requestBody: req.body}));
    } catch (err) {
      res
        .status(500)
        .send(JSON.stringify({message: err, requestBody: req.body}));
      transaction.end();
      console.error('Transaction terminated.');
    } finally {
      database.close();
    }
  });
};