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?
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.
MongoDB doesn't support transactions, but saving one document is atomic. So, it is better to design you database schema in such a way, that all the data needed to be saved atomically will be placed in one document.
However, not all databases support transactions that work across multiple records, which can be concerning to developers who are accustomed to using them. But, MongoDB supports multi-document MongoDB ACID transactions for the use cases that require them.
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).
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#.
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.
MongoDB 4.0 will add support for multi-document transactions.
https://www.mongodb.com/transactions
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With