Results 1 to 7 of 7

Thread: A multithreading question

  1. #1
    Join Date
    Apr 2008
    Location
    Karaj,Iran
    Posts
    43
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default A multithreading question

    I wanted to know if QSqlQuery.exec() is atomic or not? I mean suppose I have a query which has to bring thousands of records,so it takes some time,can I leave the the thread which has the QSqlQuery.exec() and return to it when it has brought all the records?

  2. #2
    Join Date
    Feb 2009
    Location
    Italy
    Posts
    15
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: A multithreading question

    Hi,

    I suggest you to take a look here:

    http://lists.trolltech.com/qt-intere...ad00370-0.html

    and here:
    http://www.potu.com/man/doc.trolltec...4.0/qtsql.html

    in particular:
    Qt Code:
    1. QSqlDatabase::database().transaction();
    2. QSqlQuery query;
    3. query.exec("SELECT id FROM employee WHERE name = 'Torild Halvorsen'");
    4. if (query.next()) {
    5. int employeeId = query.value(0).toInt();
    6. query.exec("INSERT INTO project (id, name, ownerid) "
    7. "VALUES (201, 'Manhattan Project', "
    8. + QString::number(employeeId) + ")");
    9. }
    10. QSqlDatabase::database().commit();
    To copy to clipboard, switch view to plain text mode 

    Transactions can be used to ensure that a complex operation is atomic (for example, looking up a foreign key and creating a record), or to provide a means of canceling a complex change in the middle.

    Regards,
    Giuseppe 'Evilcry' Bonfa'
    Happiness depends on being self-sufficient, and a master of mental attitude, self-sufficiency is achieved by living a life of Virtue - Cynics

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

    sepehr (1st February 2009)

  4. #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: A multithreading question

    QSqlQuery::exec() is blocking (synchronous) but not atomic, the OS can interrupt the thread and run another thread inbetween. Note that you shouldn't share a single database connection across threads anyway, so there shouldn't be problems.

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

    sepehr (1st February 2009)

  6. #4
    Join Date
    Apr 2008
    Location
    Karaj,Iran
    Posts
    43
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: A multithreading question

    Quote Originally Posted by wysota View Post
    Note that you shouldn't share a single database connection across threads anyway.
    I'm planning to access 5 tables in my database with relatively huge data,do you think that's a good idea to establish 5 connections?I mean is it normal?

  7. #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: A multithreading question

    It depends what you want to do. If you want to do that from 5 threads then you need 5 connections. If you want to do that from a single thread then one connection should be sufficient. Unless of course you will be accessing the database constantly - then the database might not send data fast enough, especially if it is on a remote machine. Then you might want to use more than one connection but this doesn't mean you will be able to process the data faster, it depends on the database - if it has to lock a table or the whole database, all other connections will have to wait until the lock is opened so probably the whole application would work even slower than with one connection.

  8. #6
    Join Date
    Apr 2008
    Location
    Karaj,Iran
    Posts
    43
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: A multithreading question

    Quote Originally Posted by wysota View Post
    if it has to lock a table or the whole database, all other connections will have to wait until the lock is opened so probably the whole application would work even slower than with one connection.
    since 5 connections are trying to work on 5 different tables,if a connection doesn't lock the whole database I wouldn't have any problems,but if one connection locks the whole database I would have performance issues,could you tell me when a connection locks the whole database?I mean are there certain tasks that need to lock the whole database?what are those?or it's some kind of setting that should be configured in mysql or the connection made by qt?
    ---
    A situtation never gets so complicated that it can't get any more complicated!

  9. #7
    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: A multithreading question

    Quote Originally Posted by sepehr View Post
    since 5 connections are trying to work on 5 different tables,if a connection doesn't lock the whole database I wouldn't have any problems,
    What if you have more than one client?

    but if one connection locks the whole database I would have performance issues,could you tell me when a connection locks the whole database?
    I'm sorry but this is galaxies away from the scope of this forum It all depends on the database you use so it's best to ask at a forum related to proper database backend. I'd say transactions might cause database locks but this is just a guess.

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

    sepehr (2nd February 2009)

Similar Threads

  1. SQL Question
    By ^NyAw^ in forum Qt Programming
    Replies: 5
    Last Post: 8th April 2008, 19:36
  2. Exceptions / setjmp/longjmp question
    By Aceman2000 in forum Qt Programming
    Replies: 3
    Last Post: 13th March 2008, 17:14
  3. Replies: 2
    Last Post: 21st February 2008, 22:35
  4. QThread exit()/quit() question
    By TheKedge in forum Qt Programming
    Replies: 1
    Last Post: 28th August 2006, 14:38

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.