Aborting a transaction does not rollback an inserted document

I’m currently experimenting with transactions and experiencing some issues which raised the following questions:

  1. Should a document be inserted inside a transaction without calling CommitTransaction?
  2. If the above is true, should a document be removed which was inserted in the same transaction when calling AbortTransaction?

My implementation with the latest MongoDB.Driver (2.10.4) looks as follows:

var client = new MongoClient(...);
var database = client.GetDatabase("db1");
var session = client.StartSession();
var options = new TransactionOptions(ReadConcern.Local, ReadPreference.Primary, WriteConcern.W1);
session.StartTransaction(options);
var collection = database.GetCollection<ConnectionEntity>("connections");
var newConnection = new ConnectionEntity() 
{
  Id = "1234",
  Name = "test1234"
};
collection.InsertOne(newConnection);
session.AbortTransaction();

I expected that the new document should not be inserted in the first place or should be removed after calling AbortTransaction.

Any hints would be much appreciated.

Hi,

I believe you need to use the other InsertOne() that uses client session. If you’re using the InsertOne() method without specifying client session, the insert was performed outside of the session/transaction, so it won’t be rolled back since the session/transaction didn’t know about it.

There are some code examples using various languages (including C#) in the Transactions page.

Best regards,
Kevin

3 Likes

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.