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.
Aditional information:
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
My code:
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!