1

I'm using SQLite3 on an embedded system and on x86 in a Qt application. I'm experiencing the common error "Database is locked" when multiple threads try to read/write the database.

I read this article suggested in some other answers, but I'm creating a different connection for each thread.

By adjusting a bit the QSQLITE_BUSY_TIMEOUT option (to a very large value: 10000000) I solved this problem on the x86 system and on the embedded system, but in the latter case only when not using transactions. Unfortunately I need to use transactions for all the work of each thread.

My question is: isn't it supported by SQLite3 to read/write from/to the database concurrently when using transactions? Why doesn't it simply wait all the necessary time to acquire the lock? Maybe I haven't set it up correctly?

Luca Carlon
  • 9,546
  • 13
  • 59
  • 91

1 Answers1

3

Read BEGIN TRANSACTION statement of SQL. It explicitly says that the default transaction behavior is deferred which explains the error that you are seeing. Also read this link for another good explanation.

So you need to start your SQL as "BEGIN IMMEDIATE TRANSACTION"and everybody else must do the same.

You can find source code example here. Pay attention to

bool SqlEngine::beginTransaction()

method and do the same in your code.

Community
  • 1
  • 1
O.C.
  • 6,711
  • 1
  • 25
  • 26
  • I'm finding it difficult to understand why this is working and DEFERRED was not OK, but the documentation you provided is very clear so I'll get it sooner or later :-) Anyway, it is working now. Thanks! – Luca Carlon Jun 17 '11 at 11:06