|
|
Greetings,
I can suggest two options:
(1) Use only one QSqlDatabase instance and protect the DB access with a
mutex. You will however have to keep the lock for the duration of a
transaction.
(2) Specify the 'connectionName' parameter when you add the database. If
you don't, the same connection name will be used and consequently the
same underlying pointer.
If you use the connection name, you will have two separate clients
communicating with your DB. It is then up to your DB to make sure that
you have atomic transactions.
As a rule I always specify my own connection names, just to be safe.
I would use option 2, since you will increase latency of your code if
you take a while to process your results after a select.
PS: I would definitely stay away from exposing the constructors of
QSqlDatabase.
Hi everybody,
I`m quite a qt-newbie and I might be missing something very very obvious,
but I would be very greateful if somewone nevertheless gave me a
hint... :-)
I am using QThread to establish two different threads in my program. Both of
them need access to a MySQL database.
If I use QSqlDatabase::addDatabase to establish connection in both of them,
the program crashes with segmentation fault, any time the second thread
issues an "UPDATE" statement via QSqlQuery (whereas "SELECT" statements
work perfectly right!).
After I figured out, that both threads use the same instance of QSqlDatabase
and that might causes the problem, I did the hack to
create a derived class from QSqlDatabase making the constructors of
QSqlDatabase public.
Now I instanciate QSqlDatabase in the main thread via
QSqlDatabase::addDatabase and in the second thread via the constructor of
my derived class.
Now the program seems to work, but I still get some database related crashes
after database access in both threads, even if the second thread is already
finished running! (sometimes only an "error connecting to database" occurs,
and sometimes "driver not loaded" occurs)
I am very suspicious of my hack to create these errors.
Now my question:
Is there a CLEAN(hack-free) way to use QSqlDatabase in more than one thread
at the same time? Or does someone know a hack that works without errors?
By the way I use the "QMYSQL3" driver and QT free edition 3.3.3-4.1 (I tried
both, the version issued with the debian linux distribution and a
self-compiled version)
Thank you very much for answers!
--
Jan Pool
Stone Three Signal Processing (Pty) Ltd
Tel: +27-21-8513123
Fax: +27-21-8513127
WWW: http://www.stonethree.com
Today's mighty oak is just yesterday's nut that held its ground.
--
List archive and information: http://lists.trolltech.com/qt-interest/
|
|