multithreading - Using a single QSqlDatabase connection in multiple qt threads -


i have multi threaded qt application has multiple threads accessing single database. required create separate qsqldatabase connections performing select / insert / update in each thread?

from qt documentation, unable understand if following guideline discouraging above approach suggested:

"a connection can used within thread created it. moving connections between threads or creating queries different thread not supported."

i have practically tried using same connection in multiple qthreads , works fine practically wanted understand if correct thing do.

fyi, using sqlite3 within qt (using qtsql api) understand supports serialized mode default: https://www.sqlite.org/threadsafe.html

the reason want use same connection name in multiple threads because when tried using different connections same database on multiple threads , performed select / insert / update, got database locked issue quite frequently. however, on using same connection in multiple threads, issue got eliminated completely.

kindly guide on same.

regards,

saurabh gandhi

the documentation not merely discouraging it, flatly states must not (emphasis mine):

a connection can used within thread created it.

so, no, you can't use 1 connection multiple threads. might happen work, it's not guaranteed work, , you're invoking amounts undefined behavior. it's not guaranteed crash either, mind you.

you need either:

  1. serialize access database on end, or

  2. change connection parameters locks don't reject query block until database becomes available. i'm not quite sure database locked "issue" is: should never see error code (i presume sqlite_locked) if use multiple connections. sqlite 3 can used multiple threads, shouldn't require effort on end other enabling multithreading , using separate connections.


Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -