.net MongoDB 交易?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2655251/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 14:10:26  来源:igfitidea点击:

MongoDB transactions?

.nettransactionsmongodbnorm

提问by Arnis Lapsa

Playing around with MongoDB and NoRM in .NET.

在 .NET 中使用 MongoDB 和 NoRM。

Thing that confused me - there are no transactions
(can't just tell MongoConnection.Begin/EndTransactionor something like that).

让我感到困惑的事情 - 没有交易
(不能只是告诉MongoConnection.Begin/EndTransaction或类似的事情)。

I want to use Unit of work pattern and rollback changes in case something fails.

我想使用工作单元模式并在出现故障时回滚更改。

Is there still a clean way how to enrich my repository with ITransaction?

是否还有一种干净的方法可以使用 ITransaction 丰富我的存储库?

采纳答案by mdirolf

MongoDB doesn't support complex multi-document transactions. If that is something you absolutely need it probably isn't a great fit for you.

MongoDB 不支持复杂的多文档事务。如果这是您绝对需要的东西,它可能不适合您。

In most cases, however, we've found that complex transactions aren't a requirement. All operations in MongoDB are atomic on a single document, and we support nice update modifiers, which make a lot of operations that would need a transaction easy to implement (and fast).

但是,在大多数情况下,我们发现不需要复杂的事务。MongoDB 中的所有操作在单个文档上都是原子的,并且我们支持良好的更新修饰符,这使得许多需要事务的操作易于实现(并且快速)。

回答by rystsov

That is true that MongoDB does't support transaction out of the box, but you can implement optimistic transactions on your own. They fit fine the unit of work. I wrote an java example and some explanation on a GitHubso you can easily repeat in C#.

确实,MongoDB 不支持开箱即用的事务,但您可以自己实现乐观事务。它们非常适合工作单元。我在GitHub 上写了一个 java 示例和一些解释,以便您可以轻松地在 C# 中重复。

回答by Grigori Melnik

As of v4.0, MongoDB supports multi-document ACID transactions. Through snapshot isolation, transactions provide a globally consistent view of data, and enforce all-or-nothing execution to maintain data integrity. For more info, see https://www.mongodb.com/transactions

从 v4.0 开始,MongoDB 支持多文档 ACID 事务。通过快照隔离,事务提供全局一致的数据视图,并强制执行全有或全无执行以维护数据完整性。有关更多信息,请参阅https://www.mongodb.com/transactions

In 4.2, MongoDB will also support sharded transactions.

在 4.2 中,MongoDB 还将支持分片事务。

In this blog post, I also outline our journey to multi-document ACID transactions, in case if you are interested in the history and our reasoning.

在这篇博文中,我还概述了我们的多文档 ACID 事务之旅,以防您对历史和我们的推理感兴趣。

回答by Benjamin Lorenz

MongoDB 4.0 will add support for multi-document transactions.

MongoDB 4.0 将添加对多文档事务的支持。

https://www.mongodb.com/transactions

https://www.mongodb.com/transactions

回答by Samuel Morgan

FYI - this has now changed

仅供参考 - 现在已经改变了

using (var session = mongoDbContext.MongoDatabase.Client.StartSession())
            {
                var itemAuthRepo = (Repository<ItemAuthorization, ObjectId>)mongoDbContext.ItemAuthorizations;
                var calendarRepo = (Repository<CalendarEvent, ObjectId>)mongoDbContext.Calendars;

                if (itemAuthRepo != null && calendarRepo!=null)
                {
                    session.StartTransaction();

                    try
                    {
                        itemAuthRepo.Collection.InsertOne(session, newItemAuthorization);
                        calendarRepo.Collection.InsertOne(session, cal);
                        session.CommitTransaction();
                    }
                    catch (Exception ex)
                    {
                        session.AbortTransaction();
                        throw;
                    }
                }
                else
                {
                    throw new Exception("IRepository was not casted to Repository");
                }
            }

回答by Ayberk

Yes, there are multiple ways to apply Unit of Work pattern for MongoDB depending on the database version.

是的,有多种方法可以根据数据库版本为 MongoDB 应用工作单元模式。

Until MongoDB 4.0, there was no support for multi-document ACID transactions. Then developers has used "two-phase commit protocol" (single database) and "three-phase commit protocol" (non-blocking on distributed databases) to create their own transaction layer which has provided data consistency but notall-or-nothing execution to maintain data integrity. So that way has taken down the performance.

在 MongoDB 4.0 之前,不支持多文档 ACID 事务。然后开发人员使用“两阶段提交协议”(单数据库)和“三阶段提交协议”(分布式数据库上的非阻塞)来创建自己的事务层,提供数据一致性但不是全有或全无执行以保持数据完整性。所以这种方式已经降低了性能。

MongoDB 4.0 has added support for multi-document ACID transactions.

MongoDB 4.0 添加了对多文档 ACID 事务的支持。

Sources:

资料来源:

https://en.wikipedia.org/wiki/Two-phase_commit_protocol

https://en.wikipedia.org/wiki/Two-phase_commit_protocol

https://en.wikipedia.org/wiki/Three-phase_commit_protocol

https://en.wikipedia.org/wiki/Three-phase_commit_protocol

https://www.mongodb.com/transactions

https://www.mongodb.com/transactions

回答by str

Some notes for the record.

一些笔记以供记录。

While MongoDB does not have transaction support, it supports atomicity on a single document:

虽然 MongoDB 不支持事务,但它支持单个文档的原子性

MongoDB does have support for atomic operations within a single document. Given the possibilities provided by nested documents, this feature provides support for a large number of use-cases.

MongoDB 确实支持单个文档中的原子操作。鉴于嵌套文档提供的可能性,此功能为大量用例提供支持。

Also, the manual entry about "Isolate Sequence of Operations" might be interesting. E.g.:

此外,关于“隔离操作序列”的手册条目可能很有趣。例如:

however, you can isolate a single write operation that affects multiple documents using the isolation operator.

但是,您可以使用隔离运算符隔离影响多个文档的单个写入操作。

回答by shau

You can use instead MongoDb TokuMX.

您可以改用 MongoDb TokuMX。

http://www.tokutek.com/products/tokumx-for-mongodb/

http://www.tokutek.com/products/tokumx-for-mongodb/

TokuMXTM is an open source, high-performance distribution of MongoDB that has dramatically improved performance and operational efficiency compared to basic MongoDB. TokuMX is a drop-in replacement for MongoDB, and offers 20X performance improvements, 90% reduction in database size, and support for ACID transactions with MVCC.

TokuMXTM 是 MongoDB 的开源、高性能发行版,与基本 MongoDB 相比,它显着提高了性能和运行效率。TokuMX 是 MongoDB 的直接替代品,性能提升 20 倍,数据库大小减少 90%,并支持使用 MVCC 的 ACID 事务。