在多个集合上执行事务时,MongoDB Atlas错误(代码8000) [英] MongoDB Atlas Error while performing transaction on multiple collections (code 8000)

查看:174
本文介绍了在多个集合上执行事务时,MongoDB Atlas错误(代码8000)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Mongo DB Node JS驱动程序在Mongo DB Atlas M0实例上执行事务(如

I'm trying to perform a transaction on a Mongo DB Atlas M0 instance from Mongo DB Node JS driver (as described here) and I'm getting the following error:

code:8000
codeName:"AtlasError"
errmsg:"internal atlas error checking things: Failure getting dbStats: read tcp 192.168.254.78:50064->192.168.254.78:27000: i/o timeout"
message:"internal atlas error checking things: Failure getting dbStats: read tcp 192.168.254.78:50064->192.168.254.78:27000: i/o timeout"
name:"MongoError"

我已经搜索了一段时间,找不到任何解决方法的线索.

I've been searching for a while now and can't find any clue on how to solve this.

附加信息:

  • 将第二个操作添加到 交易.

  • The error is thrown after adding the second operation to the transaction.

如果我删除所有其他操作,而只留下一个(不是
不管哪个都可以).

If I remove all the other operations and leave only one (doesn't
matter which) it works fine.

如果我将操作顺序更改为任何顺序,则错误为
仍在添加第二项操作.

If I change the order of the operations (to any order) the error is
still on adding the second operation.

如果所有操作都对同一数据库和集合执行,则可以正常工作

If the operations are all performed to the same db and collection, it works fine

我的代码:

async function connect () {

if (dbClient !== null && dbClient.isConnected()) {
    console.log('Reusing db connection => ' + JSON.stringify(dbClient));
  } else {
      console.log('Connecting to database');
      dbClient = await MongoClient.connect(url, { useNewUrlParser: true });
      console.log('Successfully connected to database');
  }
}

async function insertDocuments(document1, document2, document3) {

  try {
    await connect();
  } catch (error) {
    throw error
  }

  let session = dbClient.startSession();

  session.startTransaction({
    readConcern: { level: 'snapshot' },
    writeConcern: { w: 'majority' }
  });

  const collection1 = dbClient.db('mydbname').collection('collection1');
  const collection2 = dbClient.db('mydbname').collection('collection2');
  const collection3 = dbClient.db('mydbname').collection('collection3');
  const logsCollection = dbClient.db('mydbname').collection('logs');

  await collection1.replaceOne(
    { _id: document1._id },
    document1,
    {
      upsert: true,
      session
    }
  );
  await collection2.replaceOne(
    { _id: document2._id },
    document2,
    {
      upsert: true,
      session
    }
  );
  await collection3.replaceOne(
    { _id: document3._id },
    document3,
    {
      upsert: true,
      session
    }
  );
  await logsCollection.updateOne(
    { _id: document1._id },
    { $unset: { estoque: '' } },
    { session }
  );

  try {
    await commitWithRetry(session);
  } catch (error) {
    await session.abortTransaction();
    throw error;
  }
}

async function commitWithRetry(session) {
  try {
    await session.commitTransaction();
    console.log('Transação gravada com sucesso');
  } catch (error) {
    if (
      error.errorLabels &&
      error.errorLabels.indexOf('UnknownTransactionCommitResult') >= 0
    ) {
      console.log('Transação não realizada, tentando novamente ...');
      await commitWithRetry(session);
    } else {
      console.log('Erro ao gravar no banco de dados ...');
      throw error;
    }
  }
}

关于如何解决此问题的任何想法?预先感谢!

Any idea on how to fix this? Thanks in advance!

推荐答案

如评论中所述,根据MongoDB团队的说法,此问题已在4.0.5版本上解决

As mentioned in comments, according to MongoDB team this issue has been solved on version 4.0.5

我已经测试过与问题相同的代码,现在可以正常工作了.

I've tested the same code posted on my question and it now works fine.

对于面临相同问题的任何人,请确保使用等于或高于4.0.5的版本.

For anyone facing the same issue, make sure to be using a version equal to or higher than 4.0.5.

这篇关于在多个集合上执行事务时,MongoDB Atlas错误(代码8000)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆