Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-2339

Disposing client session after transaction commit throws exception

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Trivial - P5 Trivial - P5
    • 2.7.1
    • Affects Version/s: 2.7.0
    • None
    • Environment:
      Windows 10, C#, server version 4.0

      The following snippet of code throws an InvalidOperationException with message "Cannot call AbortTransaction after calling CommitTransaction."

       

      using (var session = await database.Client.StartSessionAsync().ConfigureAwait(false))
      {
      	try
      	{
      		session.StartTransaction();		IMongoCollection<TDatabaseType> scheduleCollection = iface.database.GetCollection<TDatabaseType>(CollectionID);
      		await scheduleCollection.InsertOneAsync(session, newObject).ConfigureAwait(false);		await session.CommitTransactionAsync().ConfigureAwait(false);
      	}
      	catch
      	{
      	    await session.AbortTransactionAsync();
      	    throw;
      	}
      }

      The exception is thrown on disposal of the client session, and upon closer inspection I found the following:

      Dispose() of CoreSession:

      if (this._currentTransaction != null)
      {
      	try
      	{
      		this.AbortTransaction(CancellationToken.None);
      	}
      	catch
      	{
      	}
      }
      

      In the Dispose function, a check is made to see if there is any transaction and if so it is aborted. However, after creating and commiting a transaction, _currentTransaction is not null but its state is "Committed". Could it be that a check for the state of _currentTransaction is missing, or am I using it wrong?

       

       

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            wouterdk Wouter De Keersmaecker
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: