2

I'm developing a small app with multithreading. I decided to use LiteDB for storing data. I was able to read, update, delete and insert on my database successfully . However, when I tried to add another thread which is also going to use same database and same table throws an exception saying that

System.IO.IOException: 'The process cannot access the file 'C:\Users\Soyuz\TestApp\bin\Debug\Soyuz.db' because it is being used by another process.'.

This is how I established a connection to my db:

using (var db = new LiteDatabase(@"Soyuz.db")) { }

Here the document says, LiteDB offers 2 types of connections. I suppose I have to use shared one since I will have to access the same database from different threads.

https://www.litedb.org/docs/connection-string/

but when I tried this code;

using (var db = new LiteDatabase(@"Soyuz.db; Connection=shared")) { }

or

using (var db = new LiteDatabase(@"Soyuz.db; Mode=Shared")) { }

this time, it throws another exception saying that

System.ArgumentException: 'EngineSettings must have Filename or DataStream as data source'

Can someone who has an experience with LiteDB help me out with this please?

Cute Bear
  • 3,223
  • 13
  • 44
  • 69
  • 2
    Shared mode is only needed when you access the .db file from different *processes*. Very expensive, concurrency is hard to come by. All that you need to do is share the "db" variable to share between threads. For smallish programs that can be as easy as declaring the variable public static, open the dbase before starting the threads. – Hans Passant Mar 07 '20 at 15:40

1 Answers1

10

You missed Filename, use this for v5:

using (var db = new LiteDatabase(@"Filename=Soyuz.db; Connection=shared")) { }

or this for v4

using (var db = new LiteDatabase(@"Filename=Soyuz.db; Mode=Shared")) { }
Pethical
  • 1,472
  • 11
  • 18