SQLite "Database is locked" error in multithreads application
Asked Answered
N

3

9

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?

Nammu answered 22/11, 2011 at 13:43 Comment(2)
Do you check that the database is been used on another thread before inserting rows?Niemeyer
I checked this by lock {} block in the code. So I fully convinced of DB free from other threads. And yes, I checked that before problem occured in thread X, DB is been used in thread Y.Nammu
N
6

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

Nammu answered 22/6, 2012 at 1:53 Comment(0)
E
0

sqlite is designed to be like this "locking" hence the lite in the name. It is designed for just the one client connection.

But what you could do use more than one database file for different areas of your application, which maybe stall the issue till your userbase grows again.

Earthy answered 22/11, 2011 at 14:40 Comment(4)
I know what SQLite like. That's why I used lock() {} mechanism to avoid concurrence connections. As I understand, all must work like this: my code { lock {SQLite code} }; my code { lock {SQLite code}}; ... SQLite code work must finish inside lock block. But SQLite reported, that it finished (control back to my application)! So application left lock block and started another. But SQLite still continue operation. SQLite deceives my application about its state.Nammu
there might be a slight discrepancy between sqlite thinking it is ready and the filesystem having finished with it. Does the file eventually unlock after a period of time? or have your further attempts to access it kept it in a lock?Earthy
If you mean does -journal file dissapear after some period of time - no, it's not. I still see -journal empty file (0 bytes) after any time period. And yes, I tried repeate failed Session.BeginTransaction call after some time period but got same error: Begin failed with SQL exception. The database file is locked\r\ndatabase is lockedNammu
I'm pretty sure SQLite supports a very high degree/order of concurrency (v3 anyway). I removed all of my lock {} statements and it works as advertised. It simulates concurrency by serializing requests/commands based on the db file lock states - sqlite.org/lockingv3.html , where every request/command is wrapped in a transaction - sqlite.org/lang_transaction.html Its default isolation level, which determines what lock-mode transactions use, is (i think) Read-Commited or Read-Uncomitted. Somewhere along the way, it sounds like the OP had it set to Serialize, hence lock excepGecko
S
0

Personally I use this trick:

imagine we have program A outputting SQL insert/update or any other transaction and program B doing the same. (or 10-20 programs/threads)

I do this:

mkfifo mydbfifo
nohup sqlite3 mydb.db <mydbfifo &
nohup programA >mydbfifo &
nohup programB >mydbfifo &

and so on..

Span answered 21/8, 2023 at 12:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.