[CSHARP-3262] BulkInsert using C# and MongoDB Transactions throws WriteConflict error Created: 19/Nov/20  Updated: 20/Nov/20  Resolved: 20/Nov/20

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Task Priority: Major - P3
Reporter: Supriya Bansal Assignee: Jeffrey Yemin
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 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?



 Comments   
Comment by Jeffrey Yemin [ 20/Nov/20 ]

OK, good luck!

Comment by Supriya Bansal [ 20/Nov/20 ]

Thank you for the quick response!

I had posted this question on the Mongo community but didn't receive any response.

I will open a Support ticket for this issue.

 

 

 

Comment by Jeffrey Yemin [ 20/Nov/20 ]

Hi there, thank you for reaching out. As this sounds like a support issue, I wanted to give you some resources to get this questioned answered more quickly:

  • our MongoDB support portal, located at support.mongodb.com
  • our MongoDB community portal, located here
    -If you are an Atlas customer, there is free support offered 24/7 in the lower right hand corner of the UI.
    Just in case you have already opened a support case and are not receiving sufficient help, please let me know and I can facilitate escalating your issue.
    Thank you!

I also want to point out that this is not likely an issue with the driver itself, as it's just throwing an exception based on an error returned from the server.

Generated at Wed Feb 07 21:44:50 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.