Skip to content
Mauricio David edited this page Sep 9, 2017 · 7 revisions

LiteDB v4 supports both thread-safe and process-safe:

  • You can create a new instance of LiteRepository, LiteDatabase or LiteEngine in each use (process-safe)
  • You can share a single LiteRepository, LiteDatabase or LiteEngine instance 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.

Recommendation

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.

Clone this wiki locally