Concurrency issue while Querying
See original GitHub issueIn LiteDB 5 it appears you cannot use the result of a query as an Enumerable to pass to Parallel.Foreach if you are performing additional query operations on the database inside of the Parallel.Foreach loop.
The error message received is “System.InvalidOperationException: ‘Collection was modified; enumeration operation may not execute.’” this is thrown on var exists = col.Exists(x => x.IdentityHash == IdentityHash && x.RunId == RunId);
inside WriteObjectExists
I’m not performing any writes while this is being run. The below code can reproduce the issue with a few thousand objects in the database. I’ve tried generating the IEnumerable different ways (as shown below)
public static IEnumerable<WriteObject> GetMissingFromFirst2(string firstRunId, string secondRunId)
{
var col = db.GetCollection<WriteObject>("WriteObjects");
var list = new ConcurrentBag<WriteObject>();
var wos = col.Find(x => x.RunId == secondRunId);
Parallel.ForEach(wos, wo =>
{
if (!WriteObjectExists(firstRunId, wo.IdentityHash))
{
list.Add(wo);
}
});
return list;
}
public static IEnumerable<WriteObject> GetMissingFromFirst(string firstRunId, string secondRunId)
{
var bag= new ConcurrentBag<WriteObject>();
var identityHashes = db.Execute($"SELECT IdentityHash FROM WriteObjects WHERE RunId = @0",
new BsonDocument
{
["0"] = secondRunId
});
Parallel.ForEach(identityHashes.ToEnumerable(), IdentityHash =>
{
if (!WriteObjectExists(firstRunId, IdentityHash["IdentityHash"].AsString))
{
list.Add(GetWriteObject(secondRunId, IdentityHash.AsString));
}
});
return bag;
}
private static bool WriteObjectExists(string RunId, string IdentityHash)
{
var col = db.GetCollection<WriteObject>("WriteObjects");
var exists = col.Exists(x => x.IdentityHash == IdentityHash && x.RunId == RunId);
return exists;
}
private static WriteObject GetWriteObject(string runId, string IdentityHash)
{
var col = db.GetCollection<WriteObject>("WriteObjects");
return col.FindOne(x => x.IdentityHash == IdentityHash && x.RunId == runId);
}
Issue Analytics
- State:
- Created 4 years ago
- Comments:19 (5 by maintainers)
Top Results From Across the Web
Concurrency problems - theory and experimentation in ...
This phenomenon happens when two transactions access the same record and both updates this record. The following figure summarizes what could ...
Read more >Concurrency problems in DBMS Transactions
The unrepeatable problem occurs when two or more read operations of the same transaction read different values of the same variable. Example: In ......
Read more >Concurrency issues
Concurrency refers to the sharing of resources by multiple interactive users ... new data or updates existing data that would satisfy application A's...
Read more >Handling Concurrency and Locking with Indexes ...
Concurrency and locking issues are common challenges when working with databases, especially when using indexes and constraints to improve performance and ...
Read more >Handling Concurrency Conflicts - EF Core
The concurrency token is loaded and tracked when an entity is queried - just like any other property. Then, when an update or...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@gfs, nested queries will be on our roadmap!