3

This is my situation, requests are made through the browser to my application. Each request is handled by a different thread and in some cases these requests include INSERT, UPDATE, DELETE, in a sqlite database. What I do not understand is how to manage the connection to the db.

For each thread I have to use a new connection (and thus open-close connection) making sure that there are never two connections with the same name? Or should I create a single connection to the DB and I have to use a mutex to avoid concurrency issues? I'm using QTSql library and SQLite as database

Sorry for my bad english.

bartbrat
  • 53
  • 1
  • 5

1 Answers1

1

Connections can only be used from within the thread that did the creation. So I would highly discourage from using a single connection. It also depends on the database driver if it allows concurrent operations.

If you are using transactions you have to keep in mind that transactions for SQLite are not thread-safe in Qt by default (DEFERRED mode). You can init a thread safe transaction manually as far as I know (BEGIN IMMEDIATE TRANSACTION query).

nobody
  • 19,814
  • 17
  • 56
  • 77
OnWhenReady
  • 939
  • 6
  • 15
  • But what happens when two different connection to same database will edit the same data? I have to use mutex to avoid concurrency? – bartbrat Jun 06 '14 at 14:54
  • Please read this post: http://stackoverflow.com/questions/1680249/how-to-use-sqlite-in-a-multi-threaded-application – OnWhenReady Jun 06 '14 at 16:11