Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLite DB (with WAL) locked when preparing a "select" statmement - why?

Tags:

c++

sqlite

I am seeing my reads getting blocked by the writes to the database which is in WAL mode - I'm stumped as to why.

My setup:

  • SQLite3 database, journal_mode=WAL, synchronous=NORMAL
  • Mulitple C++ processes (3 to be exact) use the database - Any method within these process open and close their own non-shared connection with sqlite3_open_v2.
  • Methods that are inserting data open the db in SQLITE_OPEN_READWRITE mode
  • Methods that read from the database (i.e. only do select statements) open the db in SQLITE_OPEN_READONLY mode

In WAL mode I believe it should be possible to have concurrent readers whilst there is a write occuring.

Yet I am seeing "database is locked" when I am preparing a select statement using sqlite3_prepare_v2

What could I be doing wrong which is causing the reader to get blocked? Am I misunderstanding what a "Read" actually is?

Any tips appreciated,

thanks :)

like image 417
Stretch Avatar asked Sep 14 '12 01:09

Stretch


People also ask

Why is SQLite database locked?

Cause of the error Normally, the error occurs when two users try to run transactions on the same tables and change the content. SQLite engine finds it abnormal and locks the database. Now, the user cannot run more transactions.

Does SQLite lock entire database?

SQLite Lock UsageBegins the transaction, locking the entire database for reading. Use this if you only want to read from the database. Begins the transaction, acquiring a "modify" lock. This is also known as a RESERVED lock.

What are SQLite Wal files?

Write-Ahead Log (WAL) Files. A write-ahead log or WAL file is used in place of a rollback journal when SQLite is operating in WAL mode. As with the rollback journal, the purpose of the WAL file is to implement atomic commit and rollback.


1 Answers

check whether you have sqlite3_reset after every sqlite3_step because this is one case that causes database is locked error. after preparing a statement with sqlite3_prepare and executing it with sqlite3_step,you need to always reset it with sqlite3_reset.

The sqlite3_reset(S) interface resets the prepared statement S back to the beginning of its program.

hope this solves your problem...!!!

like image 155
vinayak jadi Avatar answered Nov 09 '22 06:11

vinayak jadi