Results 1 to 2 of 2

Thread: Problem with Multi Thread Query

  1. #1
    Join Date
    Apr 2012
    Posts
    8
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Problem with Multi Thread Query

    I read two articles of internet "Asynchronous Database Access with Qt 4.x" from http://www.linuxjournal.com/article/9602
    I use these code to implement the program that use network for query some of data but I got some problem that is :

    1. it's warning when I reconnect to server
    QSqlDatabasePrivate::removeDatabase: connection 'test.db' is still in use, all queries will cease to work.
    QSqlDatabasePrivate::addDatabase: duplicate connection name 'test.db', old connection removed.

    How can I implement these code to connect database once without reconnect it again when clients connect to server and can connect more than one database for multithread query?

    2. If I want to return data to client in object and receive object from client side. What's the solution can solve?


    This is my attach source code.
    multiquery.zip

    Thanks for any help.

  2. #2
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Problem with Multi Thread Query

    Solution is quite simple.

    You can't add new database connection for every worker with the same name.
    You can or create single connection to each database and then get that connection by name in each worker, or you can create database connection for each worker with different name.
    Depending on what you need first and second approach may be as good.

    To remove the warning you get (following first approach from above), replace addDatabase( driver, connection_name ) with database( connection_name ) in your Worker constructor.
    The actual connection (using addDatabase() ) create in your server constructor (or anywhere else you think it appropriate):
    Qt Code:
    1. Worker::Worker(int ID, QObject* parent )
    2. : QObject( parent )
    3. {
    4. m_database = QSqlDatabase::database( "test.db" );
    5. }
    6.  
    7. Server::Server(QObject *parent) :
    8. QTcpServer(parent)
    9. {
    10. QSqlDatabase db = QSqlDatabase::addDatabase( "QSQLITE", "test.db" );
    11. db.setDatabaseName("test.db");
    12. if ( !db.open() )
    13. {
    14. qWarning() << "Unable to connect to database, giving up:" << db.lastError().text();
    15. return;
    16. }
    17. }
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to Spitfire for this useful post:

    olekhanchai (31st May 2012)

Similar Threads

  1. Multi - Thread Programming
    By bajoelkid12 in forum Qt Programming
    Replies: 1
    Last Post: 20th June 2011, 00:59
  2. Qt x11 GUI crash if multi-thread
    By wesley in forum Qt Programming
    Replies: 2
    Last Post: 21st January 2010, 09:11
  3. How to realize multi-threaded database query?
    By bangqianchen in forum Qt Programming
    Replies: 2
    Last Post: 14th November 2008, 07:15
  4. Multi-thread related problem,help me
    By mendynew in forum Qt Programming
    Replies: 2
    Last Post: 3rd November 2008, 03:02
  5. Multi thread problem
    By sreedhar in forum Qt Programming
    Replies: 5
    Last Post: 26th June 2006, 13:33

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.