-
Type: Bug
-
Resolution: Fixed
-
Priority: Trivial - P5
-
Affects Version/s: 2.7.0
-
Component/s: Session Management, Write Operations
-
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?