10

As I can read from SQLite FAQ it supports multiple processes reading (SELECT) and only one process writing (INSERT, UPDATE, DELETE) database at any moment in time:

SQLite uses reader/writer locks to control access to the database. When any process wants to write, it must lock the entire database file for the duration of its update. But that normally only takes a few milliseconds. Other processes just wait on the writer to finish then continue about their business

I'm using System.Data.SQLite adapter via c#.

Could someone expalin me plz, how exactly this process is going on?

Will this process work automatically and writing SQLiteCommand will simply wait if there is another writing SQLiteCommand already executing over the same database?

Or maybe it will throw an exception? What kind of it?

Sorry but I found no information about this mechanics :)

Thank you.

UPDATE:

I've found post saying that exception will be raised with a specific errorcode

Is that statement correct?

3
  • 1
    Not yet :) I prefer reading tutorials before coding - to get optimised and correct solutions...
    – bairog
    Mar 13, 2013 at 11:24
  • 1
    If you prefer optimized and correct solutions don't use SQLite for something it was not optimized for; it's a great database, but a great multi-user database it isn't.
    – Tim
    Mar 13, 2013 at 12:15
  • 3
    I require free and lightweight (I mean zero cofiguration and serverless) database for my project. So most "multiuser-optimized" databases are not suitable for me :)
    – bairog
    Mar 14, 2013 at 6:46

1 Answer 1

17

I've investigated it by myself:

I created a sample SQLite database c:\123.db with one table Categories containing two fields: ID (uniqueidentifier) and Name (nvarchar).

I then wrote some multi-thread code to emulate multiple write access to the database (don't forget to add a System.Data.SQLite reference to your project if you use this code):

using System;
using System.Data.SQLite;
using System.Threading.Tasks;

namespace SQLiteTest
{
    class Program
    {
        static void Main(string[] args)
        {
            var tasks = new Task[100];

            for (int i = 0; i < 100; i++)
            {
                tasks[i] = new Task(new Program().WriteToDB);
                tasks[i].Start();
            }

            foreach (var task in tasks)
                task.Wait();
        }

        public void WriteToDB()
        {
            try
            {
                using (SQLiteConnection myconnection = new SQLiteConnection(@"Data Source=c:\123.db"))
                {
                    myconnection.Open();
                    using (SQLiteTransaction mytransaction = myconnection.BeginTransaction())
                    {
                        using (SQLiteCommand mycommand = new SQLiteCommand(myconnection))
                        {
                            Guid id = Guid.NewGuid();

                            mycommand.CommandText = "INSERT INTO Categories(ID, Name) VALUES ('" + id.ToString() + "', '111')";
                            mycommand.ExecuteNonQuery();

                            mycommand.CommandText = "UPDATE Categories SET Name='222' WHERE ID='" + id.ToString() + "'";
                            mycommand.ExecuteNonQuery();

                            mycommand.CommandText = "DELETE FROM Categories WHERE ID='" + id.ToString() + "'";
                            mycommand.ExecuteNonQuery();
                        }
                        mytransaction.Commit();
                    }
                }
            }
            catch (SQLiteException ex)
            {
                if (ex.ReturnCode == SQLiteErrorCode.Busy)
                    Console.WriteLine("Database is locked by another process!");
            }
        }
    }
}

The result on my Core2Duo E7500 is that Exception is never raised!

Looks like SQLite is optimised enough for my needs (locking/unlocking is really fast and normally only takes a few milliseconds as SQLite FAQ tells us) - Great!

Note that there is no need to retrieve an integer ErrorCode for an SQLiteException - you can use a special enum ReturnCode field instead. All codes are described here.

Hope this information will help somebody.

9
  • 2
    Your code proves that multiple threads within a single process can seamlessly access the SQLite database. Were you able to test whether multiple different processes can access the same database?
    – pmont
    Jul 10, 2013 at 18:51
  • Well, first that comes to my mind is compiling that code in Test1.exe, than add Process.Start("Test1.exe") at the first line of Main and recompile it to Test2.exe. Seems to be working but I've never tested it...
    – bairog
    Jul 11, 2013 at 3:39
  • 3
    Good idea! I just tested using that approach and it works. Multi-process access works just fine with SQLite.
    – pmont
    Jul 11, 2013 at 23:13
  • Just want to supplement this. I am writing an NLog / SignalR based "Guaranteed Delivery" logging service that is using System.Data.SQLite to write to a local database for a background thread to send that data on to LogStash and I had 40 threads sending log messages to a local SignalR hub. It most definitely was throwing SQLite error (5): database is locked exceptions when that single process was processing messages from other threads of other processes.
    – James Eby
    Mar 3, 2017 at 21:10
  • 2
    @JamesEby SQlite uses internal journaling, so when something fails because a block status or other, it retries untill done. So, is not internal conding, and of course your inserts are not being lost. In fact, if you see the message, an error happend, and a retry happend later. This kind of "locking" error is common when you use threads to inserts. Aside of that, and if you have a HUGE read from threads, is to use WAL journal model, so writes and reads are in separate threads, so writes from threads don't affect possible thousand concurrent reads.
    – m3nda
    Aug 12, 2018 at 4:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.