Open u1-liquid opened 3 years ago
I would also like to understand if ILiteCollection<> is thread safe for multiple readers/writers in the same process. (Specifically v5.x)
Can one of the devs confirm we do not need to keep re-fetching the same Collection from the LiteDatabase to serve multiple threads?
Thanks.
It would be nice to hear from the devs on this. Doesn't seem like much activity on github to actually indicate any interest in questions or bug reports and remediation.
I did find that keeping an open collection between multiple threads can cause minor data integrity issues in that if one thread inserts a doc, the collection isn't actually updated yet...possibly until a Checkpoint (or the db is closed). This is a little surprising in that I'm using the same collection object across the threads, so even items in the WAL should be known by the same collection object?
Any updates to that topic?
I'm using LiteDB for per-process cache to caching complex queries results from external database. It works well basically except accessing almost same time to a collection first time by multiple threads.
e.g I have 10 tasks which have to grab same data from external database, and they fetched at almost same time, executed parallelly by 4 tasks. first at all, every tasks will checks cache before query to external database, like
because the
"TableName"
collection is not exists at first, one of them will execute query on external database, and trying to commit it to LiteDB... likethe problem happens this timing. in this batch, it seems only 1 task(seems executed query itself) can access the collection, others (3 tasks) gets below exception
... and the next batches (left 6 tasks) executing well without any problem.
I think something bad happens if access collection during creating collection, because this does not happens if I wrap
LiteDatabase.GetCollection("TableName").FindById("QueryHash")
as ReadLock, andBeginTrans() ~ Commit()
as WriteLock withReaderWriterLockSlim
.Do I need to use lock if multiple reader / writer uses single collection? Or is this a some kind of bug?