Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting error System.IO.IOException: 'The process cannot access the file'

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?

like image 363
Cute Bear Avatar asked Sep 17 '26 07:09

Cute Bear


1 Answers

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")) { }
like image 84
Pethical Avatar answered Sep 19 '26 22:09

Pethical



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!