Javascript Mongoose(或 MongoDB)中的 TransientTransactionError 是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/52153538/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is a TransientTransactionError in Mongoose (or MongoDB)?
提问by Sagnik Pradhan
I have server.jsand db.jsThe db.jsfile interacts with my database using Mongoose and I use server.jsto call functions from db.js:
我有server.js并且db.js该db.js文件使用 Mongoose 与我的数据库交互,我server.js用来从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:
我的 server.js 调用了三个函数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 articlebut can't make anything of it.
我究竟做错了什么?我找到了一篇文章,但什么也做不了。
采纳答案by Wan Bachtiar
What is a TransientTransactionError
什么是 TransientTransactionError
A TransientTransactionErroris a transactional error that is classified as temporary, and if retried it maybe 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. 
ATransientTransactionError是被归类为临时性的事务性错误,如果重试可能会成功。此外,当没有采取写锁并且事务(新数据)未反映在事务快照(先前数据)中时,在提交之前会发生 TransientTransactionError 写冲突。因此,这些错误可以完全安全地重试,直到出现是一个成功的提交。
Transactions that retry in this scenario are retried from the beginning of the transaction.
在这种情况下重试的事务是从事务的开始重试的。
Keep in mindThis 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.
请记住,此错误标签与锁定已被获取但事务无法完成其提交时发生的提交错误不同。此错误标签是 UnknownTransactionCommitResult。由于对应用程序中发生错误的位置以及可能的根本原因以及应用程序可以和/或将如何响应不同的错误类型的理解存在差异,因此对此的引用值得注意。
If you're using MongoDB supported drivers, there are two possible cause the code is getting this error:
如果您使用的是MongoDB 支持的驱动程序,则有两种可能的原因导致代码出现此错误:
- Any database command error that includes the "TransientTransactionError" error label in the "errorLabels" field.
- Any network error encountered running any command other than commitTransactionin a transaction.
- 在“errorLabels”字段中包含“TransientTransactionError”错误标签的任何数据库命令错误。
- 在事务中运行除commitTransaction以外的任何命令时遇到的任何网络错误。
The code example in MongoDB Transactions: retry-transactionshow cased how to handle TransientTransactionError. 
MongoDB Transactions: retry-transaction 中的代码示例展示了如何处理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. 
如果错误信息是MongoNetworkError,则表示瞬时事务错误与客户端和服务器之间的网络连接有关。这可能是可重试的一次性网络故障,或者没有需要网络配置的网络访问。如果客户端第一次尝试访问服务器时遇到该错误,很可能是需要进行网络配置。如果服务器在 MongoDB Atlas 上,请参阅配置白名单条目。
回答by gakeko betsi
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.
我在 MongoDB 网站的“主页 > 安全部分 > 网络访问 > 添加 IP”之后将我当前的 IP 添加到白名单。
I hope this helps.
我希望这有帮助。
回答by Singh Gaurav
If there is no security issue and you are just doing it for connecting: While setting up the IP Whitelist; format should be 0.0.0.0/0 , you will not face the issue.
如果没有安全问题并且您只是为了连接而这样做:在设置 IP 白名单时;格式应该是 0.0.0.0/0 ,你不会遇到这个问题。
Moreover, as you have answered yourself, we can add the ip for which we need access.
此外,正如您自己回答的那样,我们可以添加我们需要访问的 ip。
回答by SHASHANK JAIS
- Go to your mongoDB Atlas Dashboard
- Open Network Access(its there in side navbar)
- Click on ADD IP ADDRESS
- Click on allow from Any IP Address( it basically give access to your dynamic IP address)
- 转到您的 mongoDB Atlas 仪表板
- 打开网络访问(它在侧面导航栏中)
- 点击添加IP地址
- 单击允许从任何 IP 地址(它基本上可以访问您的动态 IP 地址)
Now you are done.
现在你完成了。
回答by Benny
I had the same issue/error, albeit on a Windows machine. Even though I thought I had started the mongodb service, I didn't see it running in Windows Services. So, I manually started the mongoDB service inside Services and then the error went away. Hope this helps!
我有同样的问题/错误,尽管是在 Windows 机器上。尽管我认为我已经启动了 mongodb 服务,但我没有看到它在 Windows 服务中运行。因此,我在 Services 中手动启动了 mongoDB 服务,然后错误消失了。希望这可以帮助!
回答by Blake Simeon
I encountered this error when running the populatedb.jsscript in the MDN tutorial for Express/NodeJS. 
我在运行populatedb.jsExpress/NodeJS 的 MDN 教程中的脚本时遇到了这个错误。
The script was looking for a db connection starting with mongodb://, however my connection string from mongo started with mongodb+srv://.  
该脚本正在寻找以 开头的数据库连接mongodb://,但是我从 mongo 的连接字符串以mongodb+srv://.  
I edited the script to check for this syntax instead, which resolved the error.
我编辑了脚本来检查此语法,从而解决了错误。
I hope this helps someone.
我希望这可以帮助别人。
回答by terary
I had similar problem... All day I was able to connect via mongoose. Then bang I started getting 'TransientTransactionError' error. I could connect to mongoDB via shell so I knew the server was up and running as expected.
我有类似的问题...一整天我都可以通过猫鼬连接。然后砰的一声,我开始收到“TransientTransactionError”错误。我可以通过 shell 连接到 mongoDB,所以我知道服务器已按预期启动并运行。
IPv6/localhost. My IP switched from IPv4 to IPv6. I resolved the issue by disabling IPv6 and getting regular IPv4 IP.
IPv6/本地主机。我的 IP 从 IPv4 切换到 IPv6。我通过禁用 IPv6 并获取常规 IPv4 IP 解决了该问题。
EDIT -- seems I can reliably re/create this issue by connecting to 'localhost' while my NIC is configured with a IPv6 IP. Changing localhost->127.0.0.1 seems to resolve the issue.
编辑——似乎我可以通过连接到“localhost”来可靠地重新/创建这个问题,而我的 NIC 配置了 IPv6 IP。更改 localhost->127.0.0.1 似乎解决了这个问题。
回答by Tarun
For me, the transient transaction error came whenever I switched from my wifi network to my phone's hotspot. If this happens to you too, go to MongoDB website where you have made your database and white list your current IP address again. This will solve your problem.
对我来说,每当我从 wifi 网络切换到手机热点时,就会出现瞬时交易错误。如果您也遇到这种情况,请访问已创建数据库的 MongoDB 网站并再次将您当前的 IP 地址列入白名单。这将解决您的问题。
回答by HassanK
If you are using MongoDB Atlas. You need to Whitelist your IP address on the Atlas Console in the security settings.
如果您使用的是 MongoDB Atlas。您需要在安全设置中的 Atlas 控制台上将您的 IP 地址列入白名单。
回答by DotBot
Make sure the server didn't ran out of storage space.
确保服务器没有耗尽存储空间。
In my case none of the suggested answers helped. I was pulling out my hair until I've noticed that the server simply ran out of storage space. Clearing up a few megabytes of storage solved the error immediately.
就我而言,建议的答案都没有帮助。我一直在拔头发,直到我注意到服务器的存储空间不足。清理几兆字节的存储空间立即解决了错误。
The TransientTransactionErroractually makes sense in this scenario due to its nature of being a temporary error that could be resolved upon an explicit retry - though it did took me a while to figure out it had to do with a storage issue.
在TransientTransactionError实际上使在这种情况下感觉由于是临时错误,可以在明确的重试来解决它的性质-尽管它确实花了我一段时间才能找出它与一个存储的问题做。


