我们目前有一个客户端应用程序,它由Windows服务和WPF应用程序组成,两者都使用LiteDB并针对.Net Framework 4.8。
Windows服务在本地系统帐户下运行,而WPF应用程序在用户帐户下运行。
我们选择升级从4.1.4 到5.0.7,希望并发性得到更好的支持。
WPF应用程序和Windows服务均使用以下连接字符串访问相同的LiteDB(读取和写入),连接方式为共享:
"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"))
{
...
}
我们似乎遇到了一个严重的问题,但找不到其他有相同/类似问题或可以修复问题的内容。
该服务频繁轮询数据库以处理和处理由WPF应用程序插入的数据,并执行读取和写入操作。 WPF应用程序也读写数据库,但基于用户交互的更为零散的时间框架。
尽管两个应用程序都以相同方式访问数据库且都指定connection=shared,但我们从两个应用程序中获得异常,因为它们似乎发生了冲突。
"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"
作为一个侧面说明,当测试这个问题并尝试找出其中的原因时,我们发现两个WPF应用程序读写同一个数据库时没有得到任何异常,然而,一旦我们将一个WPF应用程序替换为Windows服务,就开始出现异常。 有人知道如何防止这种情况发生吗?