LiteDB icon indicating copy to clipboard operation
LiteDB copied to clipboard

Potential data race

Open 6D65 opened this issue 11 years ago • 3 comments

Hi,

I'm using LiteDB with NancyFX to make a small stub app. While trying running around 20 requests all at once ran into this issue.

It looks like Nancy is spawning threads to deal with the load, and LiteDB is having issues to keep up and locks.

Is LiteDB threadsafe? Shall I use locking to get around this issue? Am I missing something?

Thanks.

An exception of type 'LiteDB.LiteException' occurred in LiteDB.dll but was not handled in user code

Additional information: Timeout. Database is locked for more than 00:01:00
   at LiteDB.DiskService.TryExec(TimeSpan timeout, Action action)
   at LiteDB.DiskService.Lock()
   at LiteDB.RecoveryService.DoRecovery(BinaryReader reader)
   at LiteDB.RecoveryService.<TryRecovery>b__0(FileStream stream)
   at LiteDB.RecoveryService.OpenExclusiveFile(String filename, Action`1 success)
   at LiteDB.RecoveryService.TryRecovery()
   at LiteDB.LiteDatabase..ctor(String connectionString)
   at NancyStub.CustomersModule.<.ctor>b__e(Object _) in c:\module.cs:line 103
   at CallSite.Target(Closure , CallSite , Func`2 , Object )
   at Nancy.Routing.Route.<>c__DisplayClass4.<Wrap>b__3(Object parameters, CancellationToken context)

6D65 avatar Sep 01 '15 12:09 6D65

Hi @6D65, LiteDB isn´t thread safe, but it´s process safe - so use a new instance on each call. Take a look here:

https://github.com/mbdavid/LiteDB/wiki/Transactions-and-Concurrency#concurrency

mbdavid avatar Sep 01 '15 12:09 mbdavid

Thanks @mbdavid . It makes sense. Will keep this in mind. I've missed this part of the wiki.

6D65 avatar Sep 01 '15 12:09 6D65

I have tried creating instance of the db everytime. I am still getting the same error. Couple of times (while loading) the calls are getting executed correctly and then the problem starts. My Wrapper class is as below:

public class LiteDbDataStore<T> : INoSqlDataStore<T>
{
    IDataStoreConfiguration liteDbConfiguration;
    public LiteDbDataStore(IDataStoreConfiguration liteDbConfiguration)
    {
        this.liteDbConfiguration = liteDbConfiguration;
    }
    private LiteCollection<T> GetLite()
    {
        var ds = new LiteDatabase(liteDbConfiguration.ConfigurationString);
        return ds.GetCollection<T>(typeof(T).Name);
    }
    public T Add(T entity)
    {            
        GetLite().Insert(entity);
        return entity;
    }

    public void Delete(Expression<Func<T, bool>> predicate)
    {
        GetLite().Delete(predicate);
    }

    public void Edit(T entity, Expression<Func<T, bool>> predicate)
    {
        GetLite().Update(entity);
    }

    public T Get(Expression<Func<T, bool>> predicate)
    {
        return GetLite().FindOne(predicate);
    }

    public IEnumerable<T> GetAll()
    {
        return GetLite().FindAll();
    }

    public IEnumerable<T> GetAll(Expression<Func<T, bool>> predicate)
    {
        return GetLite().Find(predicate);
    }

    public T GetById(object Id)
    {
        return GetLite().FindById(new BsonValue(Id));
    }

    public void Save()
    {
        throw new NotImplementedException();
    }
}

technicalganesh avatar Dec 09 '16 05:12 technicalganesh