Results 1 to 10 of 10

Thread: Threads and database connection

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #9
    Join Date
    Jan 2006
    Posts
    128
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Threads and database connection

    The problem as I see it is in this line
    Qt Code:
    1. db = QSqlDatabase::addDatabase("QMYSQL3");
    To copy to clipboard, switch view to plain text mode 
    addDatabase is a static function, which implies that is does not care in which Database object it was called; and you do not specify a specific second argument, the connectionName. The defaultvalue for this argument is "QLatin1String( defaultConnection )".

    This means all your database connections will have the same connectionName. Keep this in mind when you read the following warning, copied straigt from the docs:
    Warning: If you add a database with the same name as an existing database, the new database will replace the old one. This will happen automatically if you call this function more than once without specifying connectionName.
    The way I read that is: as soon as your second thread initializes its database object, it will create a new default connection and delete the old one. The SqlDatabase object in the first thread will point to the old now stale connection and should thus cease to work. Which is rather consistent with your symptoms.

    Changing the creation of database objects to the above mentioned method (meaning assuring that each connection/thread gets their own connectionName) should work. (Hopefully, not fully tested as I said ;-)

  2. The following user says thank you to camel for this useful post:

    SubV (14th November 2007)

Similar Threads

  1. Querying within Threads
    By bera82 in forum Qt Programming
    Replies: 11
    Last Post: 12th March 2008, 01:08

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.