Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LiteDB 5 System.IO.IOException: The process cannot access the file

We currently have a client application which consists of a Windows Service and a WPF application, both utilising LiteDB and targeting .Net Framework 4.8.

The windows service runs under the local system account and the WPF app runs under the user's account.

We made the choice to upgrade from 4.1.4 to 5.0.7 in the hopes that concurrency was better supported.

Both WPF app and win service access the same LiteDB (read and write) with connection=shared using the below connection string:

"Filename=C:\temp\Test-v5.db;password=pass1234;connection=shared"

using (var db = new LiteDatabase(@"Filename=C:\temp\Test-v5.db;password=pass1234;connection=shared"))
{
    ...
}

We seem to have come across a breaking issue however and can't seem to find anyone else having the same/similar issue or anything else that could fix it.

The service frequently polls the database to handle and process data that has been inserted by the WPF app with both read and write operations. The WPF app also reads and writes to the database but on a more sporadic time frame based on user interaction.

Despite both applications accessing the database in the same way and both specifying connection=shared, we are getting exceptions from both as they appear to be clashing.

"System.IO.IOException: The process cannot access the file 'C:\temp\Test-v5.db' because it is being used by another process.
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
   at LiteDB.Engine.FileStreamFactory.GetStream(Boolean canWrite, Boolean sequencial)
   at LiteDB.Engine.StreamPool.<>c__DisplayClass3_0.<.ctor>b__0()
   at System.Lazy`1.CreateValue()
   at System.Lazy`1.LazyInitValue()
   at System.Lazy`1.get_Value()
   at LiteDB.Engine.DiskService..ctor(EngineSettings settings, Int32[] memorySegmentSizes)
   at LiteDB.Engine.LiteEngine..ctor(EngineSettings settings)
   at LiteDB.SharedEngine.OpenDatabase()
   at LiteDB.SharedEngine.Query(String collection, Query query)
   at LiteDB.LiteQueryable`1.<ToDocuments>d__26.MoveNext()
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
   at System.Linq.OrderedEnumerable`1.<GetEnumerator>d__1.MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at Pixel_Upload_Service.UploadService.GetPendingJobsFromLiteDB(Boolean _allJobs) in D:\Users\Daniel\Desktop\LiteDB v5 Test\Upload Service\UploadService.cs:line 2654"

As a side note when testing this issue and trying to figure out what was going on, we found that we did not get any exceptions with two WPF apps reading and writing from the same database, however as soon as we swap one WPF app out for a Windows Service, we start getting exceptions.

Does anyone know how we can stop this happening?

like image 588
Daniel Avatar asked Oct 15 '25 04:10

Daniel


2 Answers

We have the same problem using LiteDb V.5.0.8

We store the database file on an external fileshare where multiple clients are trying to access it.

Basically the same code as mentioned above but on an external drive instead of an internal drive.

When we run the same code with a db path on the internal drive it works fine but with the external (Unix) drive we get the described error.

like image 60
Micha Brunner Avatar answered Oct 17 '25 17:10

Micha Brunner


It is not directly related to the starting position described in the initial question but I would like to share my experience as I was affected intermittendly from the same error message:

"The process cannot access the file '...' because it is being used by another process."

with LiteDb 5.0.9 running in ASP.NET MVC on IIS after the update from LiteDb 4.1.4.

It went away after I removed upgrade=true; from the connection string. Maybe this information is from help for somebody else.

like image 25
Andreas Schneider Avatar answered Oct 17 '25 17:10

Andreas Schneider



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!