Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a TransientTransactionError in Mongoose (or MongoDB)?

I have server.js and db.js The db.js file interacts with my database using Mongoose and I use server.js to call functions from db.js :

var mongoose = require('mongoose'); mongoose.connect('', { useNewUrlParser: true }) var Schema = mongoose.Schema;  module.exports = function () {     var db = mongoose.connection;     db.on('error', console.error.bind(console, 'connection error:'));     return db.once('open', function() {         console.log("Connected to DB")         var postschema = new Schema({             title: String,             intro: String,             body: String,             author: String,             timestamp: { type: Date, default: Date.now }         });          var post = mongoose.model('post', postschema);          return {             newPost(title, intro, body, author) {                 var newpost = new post({                     title: title,                     intro: intro,                     body: body,                     author: author                 })             },             getPostsAll() {                 post.find({}, function (err, res) {                     return (`Error:${err} Posts:${res}`)                 })             }         }     }) } 

And my server.js calls three functions from db.js :

var DB = require('./db.js') var db = DB() db.getPostsAll() db.newPost() 

I don't understand why I get this error :

connection error: { MongoNetworkError: connection 4 to black-test-shard-00-01-ewyaf.mongodb.net:27017 closed at TLSSocket.<anonymous> (E:\HTML\black-box\node_modules\mongodb-core\lib\connection\connection.js:276:9) at Object.onceWrapper (events.js:272:13) at TLSSocket.emit (events.js:185:15) at _handle.close (net.js:541:12) at TCP.done [as _onclose] (_tls_wrap.js:379:7)   name: 'MongoNetworkError',   errorLabels: [ 'TransientTransactionError' ],   [Symbol(mongoErrorContextSymbol)]: {} } 

What am I doing wrong? I found an article but can't make anything of it.

like image 840
Sagnik Pradhan Avatar asked Sep 03 '18 16:09

Sagnik Pradhan


People also ask

What is Mongoose vs MongoDB?

Mongoose is an Object Data Modeling (ODM) library for MongoDB and Node. js. It manages relationships between data, provides schema validation, and is used to translate between objects in code and the representation of those objects in MongoDB. MongoDB is a schema-less NoSQL document database.

What is transient transaction error?

A TransientTransactionError is a transactional error that is classified as temporary, and if retried it may be successful.

What is MongoDB transaction?

For situations that require atomicity of reads and writes to multiple documents (in a single or multiple collections), MongoDB supports multi-document transactions. With distributed transactions, transactions can be used across multiple operations, collections, databases, documents, and shards.


2 Answers

I had

  'MongoNetworkError',   errorLabels: [ 'TransientTransactionError' ],   [Symbol(mongoErrorContextSymbol)]: 

I added my current IP to whiteList following "main page > security section > network access > add IP" in MongoDB website.

I hope this helps.

like image 162
msahin Avatar answered Sep 25 '22 13:09

msahin


What is a TransientTransactionError

A TransientTransactionError is a transactional error that is classified as temporary, and if retried it may be successful. Furthermore, a TransientTransactionError write conflict occurs prior to a commit when no write lock has been taken and the transaction (new data) is not reflected in the transaction snapshot (previous data.) As a result, these errors are completely safe to retry until there is a successful commit.

Transactions that retry in this scenario are retried from the beginning of the transaction.

Keep in mind This error label is different than commit errors that happen when the lock has been taken but the transaction can't complete its commit. The error label for this is UnknownTransactionCommitResult. The reference to this is notable due to the difference in understanding where in your application an error is occurring and what may be the underlying cause and how the application can and or will respond due to different error types.

If you're using MongoDB supported drivers, there are two possible cause the code is getting this error:

  • Any database command error that includes the "TransientTransactionError" error label in the "errorLabels" field.
  • Any network error encountered running any command other than commitTransaction in a transaction.

The code example in MongoDB Transactions: retry-transaction show cased how to handle TransientTransactionError.

If the error message is MongoNetworkError, it means the transient transaction error is related to the network connectivity between the client and the server. This could either be a one time network glitch which is retry-able, or there is no network access which require network configuration. If the error is encountered on the first time the client trying to access the server, it is likely that there is network configuration needed. If the server is on MongoDB Atlas, please see Configure Whitelist Entries.

like image 20
Wan Bachtiar Avatar answered Sep 25 '22 13:09

Wan Bachtiar