[Qt-interest] QSqlQuery + multiple threads = crash
Xiaolei Shang
xiaolei.shang at cobham.com
Wed Jun 22 15:02:29 CEST 2011
On 22/06/2011 13:41, 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 ?
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at qt.nokia.com
> http://lists.qt.nokia.com/mailman/listinfo/qt-interest
Hi:
Since you told us that your pointer of QSqlDatabase can be considered as
static, I think you use it for bother threads. When thread 1 finishes, I
think you closed the database implicitly or explicitly. That explains
why you lost your connection in the thread 2.
Possible solutions:
1. In thread 2, adding code to check if the db is open before doing any
queries. If not, you need to open it.
2. In thread 2, you can create a complete new QSqlDatabase pointer to
open the same database. Then using this pointer to setup your queries.
Hope it helps.
Cheers
Xiaolei
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20110622/9675f201/attachment.html
More information about the Qt-interest-old
mailing list