问题
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