Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLite "Database is locked" error in multithreads application

There is a multithreads application, that works with large DB file (>600 Mb). "Database is locked" problem started when I added blob data, and started operate with >30 Kb of BLOB data per request. I think problem related to small HDD speed. It looks like SQLite deletes -journal file, one thread of my application got out of lock (because -journal file was applied and deleted), and other my thread want to do smth with DB, but SQLite still updates DB file... Sure, I can do a minute delays after each DB call, but this is not a solution, because I need more speed.

Now I use session per conversation (per thread) implementation. So there is one ISessionFactory per application object and many ISession objects.

There are my helper classes (as you can see I use IsolationLevel.Serializable and CurrentSessionContext = ThreadStaticSessionContext):

public abstract class nHibernateHelper
{
    private static FluentConfiguration _configuration;
    private static IPersistenceContext _persistenceContext;

    static nHibernateHelper() {}

    private static FluentConfiguration ConfigurePersistenceLayer()
    {
        return Fluently.Configure().Database(FluentNHibernate.Cfg.Db.SQLiteConfiguration.Standard.ShowSql().UsingFile(_fileName).IsolationLevel(IsolationLevel.Serializable).MaxFetchDepth(2)).
                Mappings(m => m.FluentMappings.AddFromAssemblyOf<Foo>()).CurrentSessionContext(typeof(ThreadStaticSessionContext).FullName);
    }

    public static ISession CurrentSession
    {
        get { return _persistenceContext.CurrentSession; }
    }

    public static IDisposable OpenConnection()
    {
        return new DbSession(_persistenceContext);
    }
}

public class PersistenceContext : IPersistenceContext, IDisposable
{
    private readonly FluentConfiguration _configuration;
    private readonly ISessionFactory _sessionFactory;

    public PersistenceContext(FluentConfiguration configuration)
    {
        _configuration = configuration;
        _sessionFactory = _configuration.BuildSessionFactory();
    }

    public FluentConfiguration Configuration { get { return _configuration; } }
    public ISessionFactory SessionFactory { get { return _sessionFactory; } }

    public ISession CurrentSession
    {
        get
        {
            if (!CurrentSessionContext.HasBind(SessionFactory))
            {
                OnContextualSessionIsNotFound();
            }
            var contextualSession = SessionFactory.GetCurrentSession();
            if (contextualSession == null)
            {
                OnContextualSessionIsNotFound();
            }
            return contextualSession;
        }
    }

    public void Dispose()
    {
        SessionFactory.Dispose();
    }

    private static void OnContextualSessionIsNotFound()
    {
        throw new InvalidOperationException("Ambient instance of contextual session is not found. Open the db session before.");
    }

}

public class DbSession : IDisposable
{
    private readonly ISessionFactory _sessionFactory;

    public DbSession(IPersistenceContext persistentContext)
    {
        _sessionFactory = persistentContext.SessionFactory;
        CurrentSessionContext.Bind(_sessionFactory.OpenSession());
    }

    public void Dispose()
    {
        var session = CurrentSessionContext.Unbind(_sessionFactory);
        if (session != null && session.IsOpen)
        {
            try
            {
                if (session.Transaction != null && session.Transaction.IsActive)
                {
                    session.Transaction.Rollback();
                }
            }
            finally
            {
                session.Dispose();
            }
        }
    }
}

And there is repository helper class. As you can see there are locks by every DB call, so concurrence DB call can't appear, for different threads too, because _locker object is static.

public abstract class BaseEntityRepository<T, TId> : IBaseEntityRepository<T, TId> where T : BaseEntity<TId>
{
    private ITransaction _transaction;
    protected static readonly object _locker = new object();

    public bool Save(T item)
    {
        bool result = false;

        if ((item != null) && (item.IsTransient()))
        {
            lock (_locker)
            {
                try
                {
                    _transaction = session.BeginTransaction();
                    nHibernateHelper.CurrentSession.Save(item);
                    nHibernateHelper.Flush();
                    _transaction.Commit();          
                    result = true;
                } catch 
                {
                    _transaction.Rollback();
                    throw;
                }
                //DelayAfterProcess();
            }
        }
        return result;
    }

    //same for delete and update 

    public T Get(TId itemId)
    {
        T result = default(T);

        lock (_locker)
        {
            try
            {
                result = nHibernateHelper.CurrentSession.Get<T>(itemId);
            }
            catch 
            {
                throw;
            }
        }
        return result;
    }

    public IList<T> Find(Expression<Func<T, bool>> predicate)
    {
        IList<T> result = new List<T>();
        lock (_locker)
        {
            try
            {
                result = nHibernateHelper.CurrentSession.Query<T>().Where(predicate).ToList();
            }
            catch 
            {
                throw;
            }
        }
        return result;
    }


}

I use previous classes like this (I call nHibernateHelper.OpenConnection() once per thread). Repository is instantiated by singletone:

using (nHibernateHelper.OpenConnection())
{
    Foo foo = new Foo();
    FooRepository.Instance.Save(foo);
}    

I tried to change IsolationLevel to ReadCommited, but this not changes a problem. Also I tried to solve this problem by change SQLite journal mode from journal to WAL:

using (nHibernateHelper.OpenConnection()) 
{
    using (IDbCommand command = nHibernateHelper.CurrentSession.Connection.CreateCommand())
    {
        command.CommandText = "PRAGMA journal_mode=WAL";
        command.ExecuteNonQuery();
    }
}

This helped on computers with fast HDD, but on some I got same error. Then I tried to add "DB update file exist" check to repository, and delay after each save/update/delete procedure:

    protected static int _delayAfterInSeconds = 1;
    protected void DelayAfterProcess()
    {
        bool dbUpdateInProcess = false;
        do
        {
            string fileMask = "*-wal*";
            string[] files = Directory.GetFiles(Directory.GetCurrentDirectory(), fileMask);
            if ((files != null) && (files.Length > 0))
            {
                dbUpdateInProcess = true;
                Thread.Sleep(1000);
            }
            else
            {
                dbUpdateInProcess = false;
            }
        } while (dbUpdateInProcess);
        if (_delayAfterInSeconds > 0)
        {
            Thread.Sleep(_delayAfterInSeconds * 1000);
        }
    }

Same solution (check for DB update file) not worked for -journal file. It reported, that -journal file was deleted, but I still got errors. For -wal file it works (as I think. I need more time to test it). But this solution seriously brake program.

Maybe you can help me?

like image 492
user809808 Avatar asked Nov 22 '11 13:11

user809808


People also ask

When SQLite database is locked?

When a database is accessed by multiple connections, and one of the processes modifies the database, the SQLite database is locked until that transaction is committed. The timeout parameter specifies how long the connection should wait for the lock to go away until raising an exception.

Could not open database file reason database is locked SQLite?

If you want to remove a "database is locked" error then follow these steps: Copy your database file to some other location. Replace the database with the copied database. This will dereference all processes which were accessing your database file.

Why SQLite Cannot open database file?

If SQLite is unable to open the database file, this means that the SQLite database you are trying to open is corrupted. There are various causes of corruption, such as file overwrite issues, file locking issues, database synchronization failures, storage media failures, and many more.


1 Answers

Answering to myself. Problem was related to .IsolationLevel(IsolationLevel.Serializable). When I changed this line to .IsolationLevel(IsolationLevel.ReadCommitted) problem disappeared.

like image 141
user809808 Avatar answered Sep 25 '22 17:09

user809808