[Qt-interest] QSqlQuery + multiple threads = crash

Sean Harmer sean.harmer at maps-technology.com
Wed Jun 22 14:49:13 CEST 2011


Hi,

On Wednesday 22 June 2011 14:41:37 Emmanuel Bourgerie wrote:
> Hi folks !
> 
> I'm having a little issue with Qt 4.7.2 (Ubuntu). I've got two threads
> instantiating both of them a QSqlQuery on the same QSqlDatabase, and
> I've got a weird result :
> 
> 1) Thread #1 starts, no problem
> 2) Thread #2 starts but waits for thread #1 (that's why I use threads, isn't
> it) 3) Thread #1 has finished, I've got a result
> 4) Thread #2 dies, telling me MySQL lost the connection
> 5) Back on thread #1, I can't fetch anything more, "MySQL has gone
> away" (not gentle)
> 
> So, maybe it's because of the way I managed my QSqlDatabase. Consider
> it's a static member (in fact it's a bit more complicated, but
> similar) typed QSqlDatabase*. When I prepare my QSqlQuery I do :
> 
> void QueryEditorWidget::run() {
>   query = QSqlQuery(editor->toPlainText(), *db);
>   model->setQuery(query);
>   emit ready();
> }
> 
> Any suggestion ?

You need to use a separate connection (QSqlDatabase) in each thread.

Sean




More information about the Qt-interest-old mailing list