-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Concurrency
LiteDB v4 supports both thread-safe and process-safe:
- You can create a new instance of
LiteRepository,LiteDatabaseorLiteEnginein each use (process-safe) - You can share a single
LiteRepository,LiteDatabaseorLiteEngineinstance across your threads (thread-safe)
In first option (process safe), you will works always disconnected from datafile. Each use will open datafile, lock file (read or write mode), do your operation and then close datafile. Locks are implemented using FileStream.Lock for both read/write mode. It's very important in this way always use using statement to close datafile.
In second option (thread-safe) LiteDB control concurrency using ReaderWriterLockSlim .NET class. With this class it's possible manage multiple reads and an exclusive write. All threads share same instance and each method control concurrency.
Single instance is much faster than multi instances because avoid all file control (open/read/check/write) and has almost no cache re-use.
If you application works in a single process (like mobile apps, asp.net websites) prefer use a single database instance. You can also use Exclusive mode (in connection string) to avoid check for database changed in each read/write operation and has no file lock.
Data Modeling
- Data Structure
- BsonDocument
- Object Mapping
- Relationships with Document References
- Collections
- FileStorage
Index
Query
Database
Version 4 changes
Shell