Search code examples
c#mongodbmongodb-.net-driver

MongoDB transaction


I have a method like below in C#:

private void Save(object)
{
   mongoCollection.Save(object);
   someotherRelationaldb.Save(object);
}

I have two DBs in which I have to save a object. One is MongoDB and another is a relational DB SQL server. If the Commit on Relational DB fails, I want to rollback the MongoDB save (I would like to maintain the order of save). What would be the proper way to rollback using the C# driver.


Solution

  • Yes,

    Now possible.

    For example Java;

    try (ClientSession clientSession = client.startSession()) {
       clientSession.startTransaction();
       collection.insertOne(clientSession, docOne);
       collection.insertOne(clientSession, docTwo);
       clientSession.commitTransaction();
    }
    

    See more about mongodb transactions