29

Playing around with MongoDB and NoRM in .NET.

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

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?

Arnis Lapsa
  • 45,880
  • 29
  • 115
  • 195
  • Accepted answer is outdated as MongoDB now supports transactions. you may check the following link where i have shared the code needed for enabling mongoDB transaction https://stackoverflow.com/a/72466573/1951420 – Siddharth Shakya Jun 01 '22 at 18:43

9 Answers9

30

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

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).

mdirolf
  • 7,521
  • 2
  • 23
  • 15
  • I didn't get that `update modifiers` part. How exactly can they help? (keep in mind that I'm complete newbie at this) :) – Arnis Lapsa Apr 17 '10 at 06:22
  • 1
    update modifiers allow you to perform certain complex operations atomically with a single operation - thinks like "increment the value of this field by x" or "add a new value to this array". – mdirolf Apr 19 '10 at 13:20
  • @mdirolf Should one refactor if he gets into a scenario where he is dependent on TRANSACTIONS ? – tusharmath Oct 05 '14 at 15:08
  • 8
    This answer is outdated. MongoDB now supports multi-document ACID transactions. https://www.mongodb.com/blog/post/mongodb-multi-document-acid-transactions-general-availability – Grigori Melnik Jun 28 '18 at 10:50
20

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 GitHub so you can easily repeat in C#.

rystsov
  • 1,868
  • 14
  • 16
7

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

In 4.2, MongoDB will also support sharded transactions.

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.

Grigori Melnik
  • 4,067
  • 2
  • 34
  • 40
  • MongoDB 4.0 RC0 is out - Check it out under Development releases at https://www.mongodb.com/download-center#development – Grigori Melnik May 24 '18 at 21:50
  • 1
    Transactions have arrived! 4.0 GA is out. https://www.mongodb.com/blog/post/mongodb-multi-document-acid-transactions-general-availability – Grigori Melnik Jun 28 '18 at 10:49
2

MongoDB 4.0 will add support for multi-document transactions.

https://www.mongodb.com/transactions

  • 1
    Instead of just providing the link, please put the key text into your answer. Otherwise if the link goes down we'll lose the answer! :) – sniperd Feb 23 '18 at 19:13
0

Some notes for the record.

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

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.

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.

str
  • 42,689
  • 17
  • 109
  • 127
0

You can use instead MongoDb TokuMX.

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.

shau
  • 145
  • 2
  • 11
0

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");
                }
            }
0

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

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 not all-or-nothing execution to maintain data integrity. So that way has taken down the performance.

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

Sources:

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

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

https://www.mongodb.com/transactions

Ayberk
  • 536
  • 2
  • 12
0

MongoDB supports transaction from version 4.0 onwards and from version 4.4 it also supports creating collections in transaction

I just enabled and tested transaction in mongoDB using free cluster provided by mongoDB atlas which uses mongoDB version 5.0

Following settings should work for mongoDB version 4.2+, although not tested yet.

pom.xml

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.7.0</version>
</parent>

<properties>
    <java.version>11</java.version>
    <mongodb.version>4.4.0</mongodb.version>
</properties>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-mongodb</artifactId>
    <version>2.7.0</version>
</dependency>

MongoConfig.java

@Configuration
public class MongoConfig
{
    @Bean
    MongoTransactionManager transactionManager(MongoDatabaseFactory 
    mongoDatabaseFactory)
    {
        return new MongoTransactionManager(mongoDatabaseFactory);
    }
}

then add @Transactional on function where you want to enable transaction.

It worked for me !

Siddharth Shakya
  • 122
  • 1
  • 10