Details
-
Task
-
Resolution: Done
-
Major - P3
-
None
-
None
-
None
-
None
Description
Hello!
I am writing an ETL tool where I would need to create 2 collection and perform a bulk insert operation. After the insert I would like to create some indexes. I am using Mongo C# Driver 2.11.2 .
I followed this blog to understand the transactions in Mongo: https://developer.mongodb.com/how-to/transactions-c-dotnet
It seems I cannot do bulk inserts on different collections with the same session. I keep getting the error :
Command commitTransaction failed: WriteConflict error: this operation conflicted with another operation. Please retry your operation or multi-document transaction...'
Then I separated out the bulk operations to individual methods and used the session to commit the transaction. The behavior is very inconsistent. Sometimes the collection is created with the records and sometimes exception about WriteConcern is thrown.
The Mongo connection string is for the Atlas Cluster with 1 primary and 2 replica nodes.
Snippet of the code:
using (var session = await MongoClient.StartSessionAsync())
|
{
|
session.StartTransaction();
|
try |
{
|
var result = await mongoCollection.BulkWriteAsync(session, listWrites, new BulkWriteOptions { IsOrdered = false }); |
response.IsSuccess = result.IsAcknowledged;
|
response.Count = result.InsertedCount;
|
await session.CommitTransactionAsync();
|
}
|
catch (Exception ex) |
{
|
await session.AbortTransactionAsync();
|
throw ex; |
}
|
Can someone help me with this issue?