如何在Meteor 1.8中使用MongoDB 4.0多文档ACID事务

时间:2019-02-13 10:48:26

标签: mongodb meteor

尝试通过这种方式进行操作:https://github.com/meteor/meteor/blob/3051150f2f5ae953f391802e73682fba613b3d46/packages/mongo/mongo_livedata_tests.js#L3431-L3487

但出现很多错误,例如:

UnhandledPromiseRejectionWarning:MongoError:给定事务编号5与任何正在进行的事务都不匹配。

我正在使用以下代码:

collection.js包含以下帮助器函数,用于将db命令包装在其他地方的事务中(所有这些都在服务器端):

const { client } = MongoInternals.defaultRemoteCollectionDriver().mongo;

export const RunInMongoTx = async function(func) {
      try {
        const session = await client.startSession();
        session.startTransaction();
        options = { session };

        func(options);

        session.commitTransaction();
      } catch (error) {
        session.abortTransaction();
        throw error;
      }
    };

然后在其他文件中,导入该函数并使用它:

import {RunInMongoTx} from "./collections";

Meteor.methods({
  "ShiftRequests.setAsDidNotTurnUp": sr => {
    const job = Jobs.findOne({ _id: sr.jobId });
    if (job.poster != Meteor.userId()) {
      throw new Meteor.Error(
        "not-job-poster",
        "Only poster can setAsDidNotTurnUp"
      );
    }

    RunInMongoTx(async options => {
      res = await EmployerFeedbacks.rawCollection().remove(
        {
          jobId: job._id,
          workerId: sr.workerId
        },
        options
      );

      res = await ShiftRequests.rawCollection().update(
        { _id: sr._id },
        { $set: { didNotTurnUp: true } },
        options
      );
    });
  }
});

1 个答案:

答案 0 :(得分:1)

我认为您的问题是您在不等待的情况下调用RunInMongoTx函数。

const { client } = MongoInternals.defaultRemoteCollectionDriver().mongo;

export const RunInMongoTx = async function(func) {
      try {
        const session = await client.startSession();
        session.startTransaction();
        options = { session };

        await func(options);

        session.commitTransaction();
      } catch (error) {
        session.abortTransaction();
        throw error;
      }
    };
import {RunInMongoTx} from "./collections";

Meteor.methods({
  "ShiftRequests.setAsDidNotTurnUp": async sr => {
    const job = Jobs.findOne({ _id: sr.jobId });
    if (job.poster != Meteor.userId()) {
      throw new Meteor.Error(
        "not-job-poster",
        "Only poster can setAsDidNotTurnUp"
      );
    }

    await RunInMongoTx(async options => {
      res = await EmployerFeedbacks.rawCollection().remove(
        {
          jobId: job._id,
          workerId: sr.workerId
        },
        options
      );

      res = await ShiftRequests.rawCollection().update(
        { _id: sr._id },
        { $set: { didNotTurnUp: true } },
        options
      );
    });
  }
});

让我知道这是否可行。