I
I
Ivan2021-12-09 16:01:33
SQL
Ivan, 2021-12-09 16:01:33

How to avoid database is locked error?

There is a tg bot on aiogram in a combination of aiosqlite and selenium, ran into a problem when scaling
When working with more than 20 browsers, it starts spamming the database is locked exception,
which tightly puts the bot itself, and starts skipping tasks.
I localized the problem, but I can’t figure out how to solve it, in the bottom line there are about 20 threads that access the database about 2-3 times per second.
Here is the update code:

async def update_balance.(rate: float, account_id: int):
    try:
        db = await aiosqlite.connect('ozon.db')
        await db.execute('''
                UPDATE balance, accounts
                SET balance.user_balance - {0}, accounts.cards + 1
                WHERE accounts.account_id = {1}
                AND balance.user_id = accoounts.user_id'''.format(2*rate, account_id)
            )
    except aiosqlite.DatabaseError as e:
        logging.error('Ошибка при записи баланса', e)
        await db.rollback()
    finally:
        await db.close()

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
galaxy, 2021-12-09
@FCKJesus

Sqlite is designed like this - there can be many reading connections, but only one writing connection at a time. Moreover, by default, the writing connection will block all readers.
In order for readers not to be blocked, you need to enable WAL . In this case, the writing connection will still be one at a time. If this does not suit you, you have chosen the wrong tool, and you should look towards normal server DBMS (mysql, postgresql). Theoretically, setting a longer timeout could help (but I don't see how to change it at all in aiosqlite) or catching the "database is locked" error and trying again.
db.execute("PRAGMA journal_mode=WAL")

D
Dmitry Gordinskiy, 2021-12-09
@DmitriyGordinskiy

How to avoid database is locked error?

Get rid of SQLite

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question