0

We are integrating Mongo DB as the database in our .NET based system. Since data consistency is required in our system, we need to support transactions. Mongo DB v4.0 supports transactions, but in order to integrate it with the .NET System.Transaction mechanism, it seems we need to implement a resource manager for Mongo with either a single phase (IPromotableSinglePhaseNotification) or a 2-phase (IEnlistNotification) commit.

  • The Mongo C# driver exposes a StartTransaction(), Commit() & Rollback() API which looks like a single phase commit API, so it looks like our resource manager should implement the single phase interface. On the other hand we need to support distribute transactions, so this suits the 2-phase-commit mechanism, but what should be the implementation of the first phase (Prepare) in this case?
  • In case there are multiple Mongo transaction participants (e.g. distributed transaction), what happens if one of the participants fails on the Mongo driver's Commit() API? What should I do with all the previous already committed participants?
Metheny
  • 1,112
  • 1
  • 11
  • 23
  • If there are multiple MongoDB transactions, if one transaction failed your code would have to catch the exception returned and handle appropriately. MongoDB is a distributed database. – Wan B. Mar 21 '19 at 01:00
  • In our case each machine has its own Mongo instance and own (different) data, so this isn't replication. We have a custom data synchronization mechanism between machines which copies different data to each one. But if it fails for one machine we need to roll back all of them. – Metheny Mar 22 '19 at 04:42
  • If that's the case, then you would have to implement your own custom 2-phrase commits. For example, use a commit flag in the document to indicate whether a document is valid or not. – Wan B. Mar 29 '19 at 00:22
  • That's a good idea, but what happens if a 2nd phase commit fails on one of the nodes (e.g. the node went down after the 1st phase) ? Wouldn't there be a data consistency issue, since some nodes have already committed the 2nd phase? – Metheny Mar 29 '19 at 19:24

0 Answers0