Mongoose(或 MongoDB)中的 TransientTransactionError 是什么? [英] What is a TransientTransactionError in Mongoose (or MongoDB)?

查看:42
本文介绍了Mongoose(或 MongoDB)中的 TransientTransactionError 是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 server.jsdb.js db.js 文件使用 Mongoose 与我的数据库交互,我使用 server.jsdb.js 调用函数:

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}`)
                })
            }
        }
    })
}

我的 server.js 从 db.js 调用三个函数:

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:HTMLlack-box
ode_modulesmongodb-corelibconnectionconnection.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.

推荐答案

什么是 TransientTransactionError

What is a TransientTransactionError

TransientTransactionError 是一个被归类为临时性的事务性错误,如果重试它可能成功.此外,当没有采取写锁定并且事务(新数据)未反映在事务快照(先前数据)中时,在提交之前会发生 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.

请记住此错误标签与在锁定已被获取但事务无法完成其提交时发生的提交错误不同.此错误标签是 UnknownTransactionCommitResult.对此的引用是值得注意的,因为在理解您的应用程序中发生错误的位置、可能的根本原因以及应用程序可以和/或将如何响应不同的错误类型方面存在差异.

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.

如果您使用的是 MongoDB 支持的驱动程序,有两种可能的原因代码收到此错误:

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

  • 在errorLabels"字段中包含TransientTransactionError"错误标签的任何数据库命令错误.
  • 在事务中运行除 commitTransaction 以外的任何命令时遇到的任何网络错误.
  • 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.

MongoDB Transactions: retry-transaction 中的代码示例显示举例说明如何处理TransientTransactionError.

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

如果错误信息为MongoNetworkError,则表示暂时性事务错误与客户端和服务器之间的网络连接有关.这可能是可重试的一次性网络故障,或者没有需要网络配置的网络访问.如果客户端第一次尝试访问服务器时遇到该错误,很可能是需要进行网络配置.如果服务器在 MongoDB Atlas 上,请参阅配置白名单条目.

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.

这篇关于Mongoose(或 MongoDB)中的 TransientTransactionError 是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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