/// <summary> /// Begins this instance. /// </summary> /// <returns></returns> /// <exception cref="MongoTransactionException"> /// Cannot start already committed transaction. /// or /// Cannot start transaction in failed state. /// or /// Cannot start transaction which is already rollbacked. /// or /// Cannot start transaction which is already started. /// </exception> public bool Begin() { switch (Status) { case MongoTransactionStatus.Committed: throw new MongoTransactionException("Cannot start already committed transaction."); case MongoTransactionStatus.Failed: throw new MongoTransactionException("Cannot start transaction in failed state."); case MongoTransactionStatus.Rollbacked: throw new MongoTransactionException("Cannot start transaction which is already rollbacked."); case MongoTransactionStatus.Started: throw new MongoTransactionException("Cannot start transaction which is already started."); } CommandResult result = MongoStaticContext.Context.Database.RunCommand(CreateBeginTransactionCommandDocument()); Status = result.Ok ? MongoTransactionStatus.Started : MongoTransactionStatus.Failed; return result.Ok; }
/// <summary> /// Rollbacks the internal. /// </summary> /// <returns></returns> private bool RollbackInternal() { if (Status == MongoTransactionStatus.Started) { CommandResult result = MongoStaticContext.Context.Database.RunCommand(CreateRollbackTransactionCommandDocument()); Status = result.Ok ? MongoTransactionStatus.Rollbacked : MongoTransactionStatus.Failed; return result.Ok; } return false; }