PDA

View Full Version : QThread and QSqlDatabase



pdoria
5th March 2008, 15:57
Hi All,

Regarding connections to a PostgreSQL outside a QThread Object...

Current setup:
The connection to the database is created in the main who in turn fires QThread objects to handle incoming socket connections and update the DB's tables with their data...

I *seem* to remember that there was a problem creating connections to a DB outside of a QThread Object and then executing queries inside the thread... is this true?

My point being the application seg-faulting every once in a while with no reproducible pattern...:confused:

Must I create a new connection to the DB for every new thread? I mean... it makes sense that once the connection is created the queries will use the app's default db object...

Already thankful for any insight,
Pedro Doria Meunier

pdoria
6th March 2008, 03:25
UPDATE:

Never mind. I've solved it.
It turns out that you cant have a shared DB connection throughout your threads.
One needs to:
a) have a unique connection to the DB on each thread
b) have a unique connection name for each thread
c) remember to close() the connection.