Results 1 to 5 of 5

Thread: QT 4.7.0 - DB connection pool - multithreaded program

  1. #1
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QT 4.7.0 - DB connection pool - multithreaded program

    Greetings,

    i am having alot of problems making a connection pool. I have made a QHash with a Qstring and a QSqlDatabase pointer.

    Qt Code:
    1. QHash<QString, QSqlDatabase* > openConnections;
    To copy to clipboard, switch view to plain text mode 

    I try to open a connection with this:
    Qt Code:
    1. for(int i=0; i<number;i++){
    2. db = new QSqlDatabase;
    3. QString DatabaseConnectionName;
    4. QString dbNameX=dbName;
    5. DatabaseConnectionName=dbNameX.append(QString::number(i));
    6. db->addDatabase("QODBC",DatabaseConnectionName);
    7. db->setDatabaseName(dbName);
    8. db->setPassword(password);
    9. db->setUserName(userName);
    10. if(!db->open()){
    11. QSqlError errDB;
    12. errDB = db->lastError();
    13. std::cout << qPrintable(errDB.text()) << std::endl;
    14. }
    15. openConnections.insert(dbName, db);
    16.  
    17. }
    To copy to clipboard, switch view to plain text mode 

    I get a driver not loaded error. If i do this for a single connection everthing works:
    Qt Code:
    1. db = QSqlDatabase::addDatabase("QODBC","TESTDB");
    2. db.setDatabaseName("TESTDB");
    3. db.setUserName("USER");
    4. db.setPassword("PASS");
    5. if( !db.open())
    6. {
    7. QSqlError errDBRev;
    8. errDBRev = db.lastError();
    9. std::cout << qPrintable(errDBRev.text()) << std::endl;
    10. }else{
    11. std::cout << "Connect worked" << std::endl;
    12. }
    To copy to clipboard, switch view to plain text mode 

    Can someone point me in the right direction.

    I need a connection pool since my program makes alot of querrys on multiple databases ( monitoring sistem ). Mostly it holds about 500 active threads that need to be connected to 2 DBs and execute specific SQLs every X seconds/minutes/hours and i know opening and closing connections is a costly procedure.

    With regards,

    Marko

  2. #2
    Join Date
    Nov 2009
    Location
    US, Midwest
    Posts
    215
    Thanks
    62
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QT 4.7.0 - DB connection pool - multithreaded program

    I am curious - what type of hardware are you running your 500 threads on?

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QT 4.7.0 - DB connection pool - multithreaded program

    Each thread has to create its own connection and the creation process has to take place inside the thread. So you can't "reuse" a connection after its thread dies. I do somehing like this:

    Qt Code:
    1. QList<QSqlRecord> threadedSql(const QSqlDatabase &db, const QString &query, const QVariantMap &arguments) {
    2. static QAtomicInt no = 0;
    3. QString connectionName = "connection"+QString::number(no++);
    4. QList<QSqlRecord> result;
    5. {
    6. QSqlDatabase localDb = QSqlDatabase::cloneDatabase(db, connectionName);
    7. if(!localDb.open()) break;
    8. QSqlQuery q(localDb);
    9. q.prepare(query);
    10. foreach(QString key, arguments.keys()) q.bindValue(key, arguments.value(key));
    11. if(!q.exec()) break;
    12. while(q.next()) { result << q.record(); }
    13. localDb.close();
    14. }
    15. QSqlDatabase::removeDatabase(connectionName);
    16. return result;
    17. }
    18. // ...
    19.  
    20. QVariantMap args;
    21. args.insert(":id", 7);
    22. QFuture<QList<QSqlRecord> > future = QtConcurrent::run(threadedSql, QSqlDatabase::database(), "SELECT * FROM TABLE WHERE id=:id", args);
    23. //...
    24. QList<QSqlRecord> result = future.result();
    To copy to clipboard, switch view to plain text mode 

    What you can do to avoid creating connections is to hold an active pool of threads (you can hold them on wait conditions or individual mutexes).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. The following user says thank you to wysota for this useful post:

    Kozy (30th January 2011)

  5. #4
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QT 4.7.0 - DB connection pool - multithreaded program

    Greetings,

    thank you for your reply.

    to clear it up my threads are active, they run forever. They do however sleep for a specified amount of time.

    So If I understand you correctly, i cannot open the connections in the main thread and store their pointers in a list/hash and then pass them to the thread that request a connection over functions...? I think this should be possible in java.

    I guess if this is not possible I will have to combine my threads into groups of threads per server, so i will not need as many connections as i do now and maybe think of keeping the connections alive. Currently i close them and clean all the resources when i don't need them anymore for the next few minutes. In my design every query has a time of sleep in the XML structure and source / destination, query, DB info...

    About the question of what kind of hardware the app is running on, it is a 4 CPU virtual machine with a small storage that holds an instance of win server 2003 that also has Apache, Tomcat and DB2 installed. On this server I also run a PHP web page where we can see status of DBs and we can also monitor app. servers...

    The app is monitoring over 70 DB2s "health" with different SQLs that let us know what is going on out in the field.

    With regards,

    Marko

  6. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QT 4.7.0 - DB connection pool - multithreaded program

    Quote Originally Posted by Kozy View Post
    to clear it up my threads are active, they run forever. They do however sleep for a specified amount of time.
    This leads me to a conclusion you don't need so many threads. If they sleep for a period of time, they can be used to do some other work. In the end you'll probably do with 10-20 threads max.

    So If I understand you correctly, i cannot open the connections in the main thread and store their pointers in a list/hash and then pass them to the thread that request a connection over functions...?
    Passing any pointers doesn't make sense as you can retrieve a handle to a connection using the connection name and calling QSqlDatabase::database().

    I think this should be possible in java.
    It's possible in C++ as well but this particular behaviour is caused by the fact how Qt implements SQL connections. They are associated with the thread that creates them and you can't do anything about it.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. The following user says thank you to wysota for this useful post:

    Kozy (31st January 2011)

Similar Threads

  1. Replies: 10
    Last Post: 22nd July 2009, 23:52
  2. Designing Multithreaded app in Qt
    By summer_of_69 in forum Qt Programming
    Replies: 3
    Last Post: 18th June 2009, 15:46
  3. wrong connection? program crashes altough it compiles
    By cbarmpar in forum Qt Programming
    Replies: 7
    Last Post: 30th September 2008, 12:48
  4. Reading from sockets in a multithreaded program
    By KoosKoets in forum Qt Programming
    Replies: 9
    Last Post: 4th April 2007, 20:43
  5. 3D rendering of a pool table
    By Israa in forum General Programming
    Replies: 5
    Last Post: 4th March 2007, 14:06

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.