I’m facing an issue while inserting simultaneously multiple documents in couchbase, using transactions.
What I have read on the couchbase documentation (batching paragraph) is to instantiate a Task for each insertion and wait for the completion of all of them (as documented here)
If I try to execute many inserts in a batch (by instantiating multiple tasks) without using transactions everythings works fine, but when I call the InsertAsync method from the AttemptContext object, an exeception will randomly be thrown.
Here I’ve created a repository where you can reproduce the error.
You can find the stack trace listed below:
Couchbase.Transactions.Error.TransactionFailedException: Transaction failed.
—> System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
at System.Linq.Enumerable.Any[TSource](IEnumerable`1 source, Func`2 predicate)
at Couchbase.Transactions.AttemptContext.InsertAsync(ICouchbaseCollection collection, String id, Object content)
at TransactionBulkInsert.Program.<>c__DisplayClass7_1.<b__0>d.MoveNext() in C:\Users\user\source\repos\TransactionBulkInsert\Program.cs:line 73
— End of stack trace from previous location where exception was thrown —
at Couchbase.Transactions.Transactions.ExecuteApplicationLambda(Func`2 transactionLogic, TransactionContext overallContext, ILoggerFactory loggerFactory, TransactionResult result)
— End of inner exception stack trace —
at Couchbase.Transactions.Transactions.RunAsync(Func`2 transactionLogic, PerTransactionConfig perConfig)
at TransactionBulkInsert.Program.SaveBatch() in C:\Users\user\source\repos\TransactionBulkInsert\Program.cs:line 64
Examining the source code I suppose is something related to _stagedMutations property of AttempContext class. In particular seems that the exception was thrown at line 424 when searching for staged mutations while another task may have changed the same collection.
Currently I’m using these nuget packages:
- CouchbaseNetClient: v3.1.7
- Couchbase.Transactions: v1.0.0
and Couchbase Server Community v.6.6.0
Do you have any suggestion?