Skip to content

Add MongoDB transactions to WordRepository multi-collection operations#4191

Merged
imnasnainaec merged 2 commits intomongo-transactionsfrom
copilot/sub-pr-4189
Mar 4, 2026
Merged

Add MongoDB transactions to WordRepository multi-collection operations#4191
imnasnainaec merged 2 commits intomongo-transactionsfrom
copilot/sub-pr-4189

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 4, 2026

WordRepository methods that coordinate writes across WordsCollection and FrontierCollection were not atomic — a failure mid-operation could leave the two collections in an inconsistent state.

Changes

  • IMongoTransaction: Added IClientSessionHandle Session { get; } so callers can pass the session handle to MongoDB collection operations within a transaction
  • MongoDbContext: Implemented Session in MongoTransactionWrapper
  • MongoDbContextMock: Stubbed Session => null! (session is never accessed in tests; WordRepository is [ExcludeFromCodeCoverage] and tests use WordRepositoryMock)
  • WordRepository: Stored _dbContext field; wrapped all 5 multi-collection methods in transactions with explicit commit/abort:
    • DeleteAllWords
    • Create(Word) / Create(List<Word>)
    • CreateAndDeleteFrontier
    • AddAndDeleteFrontier

Each method follows the pattern:

using var transaction = await _dbContext.BeginTransaction();
try
{
    await _words.InsertOneAsync(transaction.Session, word);
    await _frontier.InsertOneAsync(transaction.Session, word);
    await transaction.CommitTransactionAsync();
}
catch
{
    await transaction.AbortTransactionAsync();
    throw;
}

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.


This change is Reviewable

…hanges

Co-authored-by: imnasnainaec <6411521+imnasnainaec@users.noreply.github.com>
Copilot AI changed the title [WIP] Use transactions to coordinate updates to FrontierCollection, WordsCollection Add MongoDB transactions to WordRepository multi-collection operations Mar 4, 2026
@imnasnainaec imnasnainaec marked this pull request as ready for review March 4, 2026 17:11
@imnasnainaec imnasnainaec merged commit 8cd0c28 into mongo-transactions Mar 4, 2026
1 check was pending
@imnasnainaec imnasnainaec deleted the copilot/sub-pr-4189 branch March 4, 2026 17:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants