mbdavid / LiteDB

LiteDB - A .NET NoSQL Document Store in a single data file
http://www.litedb.org
MIT License
8.62k stars 1.25k forks source link

[BUG] System.IO.IOException: Sharing violation on path /data/user/0/... #2160

Open DamienDoumer opened 2 years ago

DamienDoumer commented 2 years ago

I'm having this issue on Litedb with Android app (built with xamarin forms),

Version: 5.0.11

Note:

FileStreamFactory.GetStream (System.Boolean canWrite, System.Boolean sequencial) System.IO.IOException: Sharing violation on path /data/user/0/

Here is how I create the single instance of my LiteDatabase:

        var baseDir = FileSystem.AppDataDirectory;
        var dbPath = Path.Combine(baseDir, options.Value.DatabaseFileName);
        var dbDir = Path.GetDirectoryName(dbPath);
        if (!Directory.Exists(dbDir))
        {
            Directory.CreateDirectory(Path.GetDirectoryName(dbPath));
        }
        Connection = new ConnectionString
        {
            Filename = dbPath,
            Connection = ConnectionType.Direct
        };
        Database = new LiteDatabase(Connection);

Here is my stacktrace:

System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.Boolean anonymous, System.IO.FileOptions options) [0x001b7] in

<9a2141e59f004b13b49be68b4ea0bbf4>:0 System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.IO.FileOptions options) [0x00000] in <9a2141e59f004b13b49be68b4ea0bbf4>:0 (wrapper remoting-invoke-with-check) System.IO.FileStream..ctor(string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare,int,System.IO.FileOptions) FileStreamFactory.GetStream (System.Boolean canWrite, System.Boolean sequencial) <.ctor>b__0 () Lazy`1[T].ViaFactory (System.Threading.LazyThreadSafetyMode mode) Lazy`1[T].ExecutionAndPublication (System.LazyHelper executionAndPublication, System.Boolean useDefaultConstructor) Lazy`1[T].CreateValue () Lazy`1[T].get_Value () StreamPool.get_Writer () LiteDB.Engine.DiskService..ctor (LiteDB.Engine.EngineSettings settings, System.Int32[] memorySegmentSizes) [0x000ab] in <7dd2c2ae6ae04572a54f6c996328226c>:0 LiteDB.Engine.LiteEngine..ctor (LiteDB.Engine.EngineSettings settings) [0x00173] in <7dd2c2ae6ae04572a54f6c996328226c>:0 ConnectionString.CreateEngine () LiteDB.LiteDatabase..ctor (LiteDB.ConnectionString connectionString, LiteDB.BsonMapper mapper) [0x00034] in <7dd2c2ae6ae04572a54f6c996328226c>:0 MyAppe.MobileApp.CacheServices.LiteDb.Shared.LiteDBConnection..ctor (Microsoft.Extensions.Options.IOptions`1[TOptions] options) [0x00055] in :0 (wrapper managed-to-native) System.Reflection.RuntimeConstructorInfo.InternalInvoke(System.Reflection.RuntimeConstructorInfo,object,object[],System.Exception&) RuntimeConstructorInfo.InternalInvoke (System.Object obj, System.Object[] parameters, System.Boolean wrapExceptions) RuntimeConstructorInfo.DoInvoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) RuntimeConstructorInfo.Invoke (System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) CallSiteRuntimeResolver.VisitConstructor (Microsoft.Extensions.DependencyInjection.ServiceLookup.ConstructorCallSite constructorCallSite, Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeResolverContext context) CallSiteVisitor`2[TArgument,TResult].VisitCallSiteMain (Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceCallSite callSite, TArgument argument) CallSiteRuntimeResolver.VisitRootCache (Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceCallSite callSite, Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeResolverContext context) CallSiteVisitor`2[TArgument,TResult].VisitCallSite (Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceCallSite callSite, TArgument argument) CallSiteRuntimeResolver.VisitConstructor (Microsoft.Extensions.DependencyInjection.ServiceLookup.ConstructorCallSite constructorCallSite, Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeResolverContext context) CallSiteVisitor`2[TArgument,TResult].VisitCallSiteMain (Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceCallSite callSite, TArgument argument) CallSiteRuntimeResolver.VisitDisposeCache (Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceCallSite transientCallSite, Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeResolverContext context) CallSiteVisitor`2[TArgument,TResult].VisitCallSite (Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceCallSite callSite, TArgument argument) CallSiteRuntimeResolver.VisitConstructor (Microsoft.Extensions.DependencyInjection.ServiceLookup.ConstructorCallSite constructorCallSite, Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeResolverContext context) CallSiteVisitor`2[TArgument,TResult].VisitCallSiteMain (Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceCallSite callSite, TArgument argument) CallSiteRuntimeResolver.VisitRootCache (Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceCallSite callSite, Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeResolverContext context) CallSiteVisitor`2[TArgument,TResult].VisitCallSite (Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceCallSite callSite, TArgument argument) CallSiteRuntimeResolver.Resolve (Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceCallSite callSite, Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope scope) ServiceProvider.CreateServiceAccessor (System.Type serviceType) ConcurrentDictionary`2[TKey,TValue].GetOrAdd (TKey key, System.Func`2[T,TResult] valueFactory) ServiceProvider.GetService (System.Type serviceType, Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope serviceProviderEngineScope) ServiceProvider.GetService (System.Type serviceType) ServiceProviderServiceExtensions.GetRequiredService (System.IServiceProvider provider, System.Type serviceType) ServiceProviderServiceExtensions.GetRequiredService[T] (System.IServiceProvider provider) MyAppe.MobileApp.App..ctor (System.Action`1[T] configuration, System.Action`2[T1,T2] dependencyServiceConfiguration) [0x00057] in <4c7de3aa76d348f2b3428d0d66c81620>:0 MainActivity.OnCreate (Android.OS.Bundle savedInstanceState) AsyncMethodBuilderCore+<>c.b__7_0 (System.Object state) SyncContext+<>c__DisplayClass2_0.b__0 () Thread+RunnableImplementor.Run () IRunnableInvoker.n_Run (System.IntPtr jnienv, System.IntPtr native__this) (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.132(intptr,intptr)

. Can someone please help? is there a trick somewhere?

timdinhdotcom commented 2 years ago

On Android, you can have multiple instance of an app running and this create problem with connection=direct. A workaround for me is that I would catch the IOException then attempt connection=shared

LiteDatabase db;
try {
    db = new LiteDatabase("Filename=abc.db");
}
catch(IOException) {
    db = new LiteDatabase("Filename=abc.db;Connection=shared");
}
DamienDoumer commented 2 years ago

Shared connection doesn't work everywhere. Android doesn't support it check this doc : https://www.litedb.org/docs/connection-string/