sqlite3 'database is locked' won't go away with retries

Posted by Azarias on Stack Overflow See other posts from Stack Overflow or by Azarias
Published on 2010-01-12T18:12:39Z Indexed on 2010/03/24 7:03 UTC
Read the original article Hit count: 225

Filed under:
|

I have a sqlite3 database that is accessed by a few threads (3-4). I am aware of the general limitations of sqlite3 with regards to concurrency as stated http://www.sqlite.org/faq.html#q6 , but I am convinced that is not the problem.

All of the threads both read and write from this database. Whenever I do a write, I have the following construct:

            try:
                Cursor.execute(q, params)
                Connection.commit()
            except sqlite3.IntegrityError:
                Notify
            except sqlite3.OperationalError:
                print sys.exc_info()
                print("DATABASE LOCKED; sleeping for 3 seconds and trying again")
                time.sleep(3)
                Retry

On some runs, I won't even hit this block, but when I do, it never comes out of it (keeps retrying, but I keep getting the 'database is locked' error from exc_info. If I understand the reader/writer lock usage correctly, some amount of waiting should help with the contention. What this sounds like is deadlock, but I do not use any transactions in my code, and every SELECT or INSERT is simply a one off. Some threads, however, keep the same connection when they do their operation (which includes a mix of SELECTS and INSERTS and other modifiers).

I would appericiate it if you could shade a light on this, and also ways around fixing it (besides using a different database engine.)

© Stack Overflow or respective owner

Related posts about python

Related posts about sqlite3