Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLAlchemy and SQLite: database is locked

I have a python script which uses the latest sqlalchemy. When i use sqlite,only sqlite, other db works well, i get the following error:

sqlalchemy.exc.OperationalError: (OperationalError) database is locked u'SELECT blabla....

Any hint?

Example from my code (simplified), i have several methods like this, to select, update and delete things:

class MyDb(object):
    def __init__(self):
        engine = create_engine("sqlite:///file", poolclass=NullPool, pool_threadlocal=True)
        engine.pool_size=1
        engine.pool_timeout = 60
        self.sess = sessionmaker(bind=engine)

    def del_stuff(self):
        sess = self.sess()
        sess.query(Stuff).delete()
        try:
            sess.commit()
        except:
            sess.rollback()

    def set_stuff(self, id, bar):
        sess = self.sess()
        sess.query(Foo).get(id).bar = bar
        try:
            sess.commit()
        except:
            sess.rollback()
like image 483
tapioco123 Avatar asked Dec 15 '12 18:12

tapioco123


People also ask

How do you fix SQLite database is locked?

You should replace the backup database with the primary database. After the successful completion of the queries, the backup file will replace the locked database. As the backup file does not have any lock, it will be ready for the new transactions and queries by the user.

Does SQLAlchemy support SQLite?

SQLAlchemy supports all the databases like that SQLite is one of the databases and it is the storage engine which can be a better way to compare the other databases like MySQL, PostgreSQL, MSSQL, Oracle to store and retrieved the data it may be both structured data from the user files and it is the python library that ...

Is SQLite and SQLAlchemy same?

SQLite is a database storage engine which can be better compared against things such as PostgreSQL, Oracle, MSSQL. It is used to store and retrieve structured data from files. SQLAlchemy is a python library that provides a object relational mapper(ORM).


2 Answers

SQLite locks the database when a write is made to it, such as when an UPDATE, INSERT or DELETE is sent. When using the ORM, these get sent on flush. The database will remain locked until there is a COMMIT or ROLLBACK.

I've mostly seen the "database is locked" error in multi-threading situations. One thread will lock the database and another thread will attempt a write of its own. If the first thread doesn't release the lock within the timeout period (4-5 seconds by default, if I recall) the OperationalError is raised on the second thread.

It can be tricky to know when a flush, and therefore a write is made to the database when the session has autoflush=True (the default setting) since any query will cause a flush. Sometimes turning on the SQL logging can help clarify when things are happening:

logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)

There is some relevant documentation here: http://docs.sqlalchemy.org/en/rel_0_9/dialects/sqlite.html#database-locking-behavior-concurrency

like image 176
Eric Smith Avatar answered Sep 17 '22 19:09

Eric Smith


Check any commits pending in database through any developer tools.

As everyone told above sqlite databases only allow one process to access it at a time. In my case, I am using DB browser for sqlite and in the same, I didn't commit a query. That's also lock the DB and will not allow the application to write to database.

like image 35
Bino Kochumol Varghese Avatar answered Sep 19 '22 19:09

Bino Kochumol Varghese