Requirements for using MongoDB transactions

◇◆丶佛笑我妖孽 提交于 2020-12-15 07:32:13

问题


When creating a new ticket I am trying to use mongo transactions so that I could rollback the transaction if at least one of the "ticket saving process" or "Publishing the ticket created event" fails.

Inside the try catch block I have manually thrown the error (throw new Error('ssomlk caused manual error');) so I could try to test whether the transaction works.

const session = await mongoose.startSession();
session.startTransaction();
try {
  await ticket.save({ session });
  throw new Error('ssomlk caused manual error');
  await new TicketCreatedPublisher(natsWrapper.client).publish({
    id: ticket.id,
    title: ticket.title,
    price: ticket.price,
    createdBy: ticket.createdBy,
  });
  await session.commitTransaction();
  session.endSession();
  res.status(201).send(ticket);
} catch (error) {
  session.abortTransaction();
  session.endSession();
  console.log(error);
  res.status(401).send({ error: error.message });
}

But I am getting the below error

MongoError: This MongoDB deployment does not support retryable writes. Please add retryWrites=false to your connection string.

According to stackoverflow answers I have included "?retryWrites=false" to my mongo URI but still no luck.

Any advise would be of great help


回答1:


First, you should be using the pattern described here (click on nodejs to see the node examples).

Second, transactions require:

  • WiredTiger storage engine
  • replica set (server 4.0+) or sharded cluster (server 4.2+)

Deployments using mmapv1 storage engine on 4.0 and standalone servers do not support transactions.



来源:https://stackoverflow.com/questions/62349032/requirements-for-using-mongodb-transactions

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!